Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto,
2002.
Lab 2: Building Application
What if the arguments given into the Application is not a valid number (such
as a string). The following will show the example. The first argument is mistyped
so that a "p" appears.
When there is an error in runtime, the error which is called Exception
is passed up to any Exception handlers exist in the object's code. When it found
none, then the top most Exception handler which is the Java Virtual Machine
will handle them, display the Exception information and its location and then
stops the execution.
C:\Lab2>java MobileSystem 10p 200 170 150 |
We could also define an Exception Handler in the way that will not stop the execution of the program, but only skip or recover some of the problematic codes.
Java have a very specific error handling (or better called Exception handling).
The way of handling exceptions is by containing the statement that submitting
Exception with a try-catch-finally block.
All the specific Exceptions are inheritance of Exception Class.
For example, the Double.parseDouble(String) static method will generate
the NumberFormatException Class.
Thus, one can prevent the Java Virtual Machine to catch the Exception by containing
the Double.parseDouble(String) method in a try-catch block.
Change the MobileSystem.java code by containing the errorneous statement in a try-catch block as shown below.
public static void main(String args[]) |
After recompilation, running the same command line will have the result of :
C:\Lab2>java MobileSystem 10p 200 170 150 Point no 1 is not valid. Cannot find its coverage. The point 170.0,150.0 covered by RBS ID:RBS-6,
Cell location at x=160.0,y=150.0 serving area= 1039.23048. |