/**
* This
is the example I was writing in class when we ran out of time
* Note
that if you reverse the order of the catch blocks this WILL
* compile. Note also that in line 11 we had to declare str a String
* not
a char because Integer.parseInt works on Strings.
*
* @author Elizabeth Adams
* date
September 2, 2008
*/
import java.util.*;
public class TE
{
public static void main (String [] args)
{
String str;
int number;
str = "5";
try
{
number = Integer.parseInt(str);
} // end try
catch (IllegalArgumentException ile)
{
System.out.println("Bad number format.");
System.out.println (ile.getMessage());
ile.printStackTrace();
} // end catch ile
// the following statement
will cause a COMPILE error because
// a NumberFormatException
can be caught by IllegalArgumentException
// which is above it in
the hierarchy (is a super class)
//
The error is shown after the catch block
catch (NumberFormatException nfe)
{
System.out.println(str + " is not a number.");
System.out.println (nfe.getMessage());
nfe.printStackTrace();
} //end catch nfe
// TE.java:21: exception java.lang.NumberFormatException
has already been caught
//
catch (NumberFormatException nfe)
// ^
// 1
error
} // end main
} // end class