Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 2: Building Application


Trap runtime errors (Exceptions)

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
Exception in thread "main" java.lang.NumberFormatException: For input string: "10p"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1213)
at java.lang.Double.parseDouble(Double.java:202)
at MobileSystem.main(MobileSystem.java:79)

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[])
{ // ... the rest of the code for (int i=0; i<(args.length/2); i++) { double x,y; try
{ x = Double.parseDouble(args[i*2]); y = Double.parseDouble(args[(i*2)+1]); } catch (NumberFormatException e )
{ System.out.println("\n\nPoint no "+ ( i + 1) +" is not valid. Cannot find its coverage."); continue; }
// ... the rest of the code
} }

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.
Neighboring RBS are:
RBS ID:RBS-4, Cell location at x=160.0,y=190.0 serving area= 1039.23048
RBS ID:RBS-5, Cell location at x=125.0,y=170.0 serving area= 1256.6370614359173
RBS ID:RBS-7, Cell location at x=125.0,y=130.0 serving area= 800.0


[Contents] [Next] [Previous]