Class 2 | Intermediate Java 30-IT-397 |
Comprehensive Example
/** Creates a new instance of ExceptionExample
*/
public ExceptionExample() {
}
// Add each value of the array.
public void compute(String[] args) {
// sum holds our total.
int sum = 0;
// loop through each
array element.
for (int i = 0; i <
args.length; i++) {
// convert the element to a number.
int number = Integer.parseInt(args[i]);
// add it.
sum += number;
}
}
// main method, called from command line.
public static void main(String args[]) {
// Make a new ExceptionExample
object, call compute.
ExceptionExample e =
new ExceptionExample();
e.compute(args);
}
}
java.lang.NumberFormatException: a
- So what happens when we pass it a String? We get this:
at java.lang.Integer.parseInt(Integer.java:414)
at java.lang.Integer.parseInt(Integer.java:463)
at ExceptionExample.compute(ExceptionExample.java:28)
at ExceptionExample.main(ExceptionExample.java:40)
Exception in thread "main"
Making Your Own Exception
- The NumberFormatException is an unchecked exception. We can still catch this, though. This is especially handy for logging and debugging purposes. We can also send a more informational message when we catch it.
- Let's change our call to e.compute in public static void main(String args[]) { to this:
try {
e.compute(args);
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException caught in ExceptionExample.compute(). Message: " + ex.getMessage());
ex.printStackTrace();
}
- Now our output is this:
NumberFormatException caught in ExceptionExample.compute(). Message: ajava.lang.NumberFormatException: a
at java.lang.Integer.parseInt(Integer.java:414)
at java.lang.Integer.parseInt(Integer.java:463)
at ExceptionExample.compute(ExceptionExample.java:28)
at ExceptionExample.main(ExceptionExample.java:41)
- Note that our more informative text now appears.
- But we still have not solved the problem of less than one argument. First, let's add this logic to the very beginning of the compute(String args[]) method:
if (args.length < 2) {
throw new Exception("Too few arguments provided. Please try again.");
}
- Try compiling it now. Forte' will tell you:
ExceptionExample.java [25:1] unreported exception java.lang.Exception; must be caught or declared to be thrown
throw new Exception("Too few arguments provided. Please try again.");
^
1 error
Errors compiling ExceptionExample.
- So, you need to declare that it trows the Exception. Change the method signature to this:
public void compute(String[] args) throws Exception {
- Again, try compiling it. Now Forte' tells you:
ExceptionExample.java [45:1] unreported exception java.lang.Exception; must be caught or declared to be thrown
e.compute(args);
^
1 error
Errors compiling ExceptionExample.- Thus, you must catch the exception. Let's add another catch() { block to our existing block. Note, we do not have to add another try, just a catch. Here's the catch block we need:
} catch (Exception ge) {
System.out.println("General Exception caught in ExceptionExample.compute(). Message: " + ge.getMessage();
ge.printStackTrace();
}
- Placement is another thing. First, try putting this before the existing NumberFormatException catch, and send it some alphabetic characters. You'll find that both exceptions - the NumberFormatException and the Exception - are handled by the Exception block. There would be no circumstance where the NumberFormatException block is called.
- Next, try putting the Exception catch after the NumberFormatException catch. Now, when a general Exception is thrown, the Exception catch handles it. When a NumberFormatException is thrown, the NumberFormatException catch handles it.
- Why? An exception will be handled by the first catch block that is either the same type as it is, or is a superclass of its type. So, you generally want to start your catches with the most specific, and work down to the most general.
- The completed code can be found here.
Created by: Brandan Jones December 17, 2001