Académique Documents
Professionnel Documents
Culture Documents
Objectives
(we will see if we have achieved them at the end of the day)
How to use methods that cause exceptions How to respond to exceptions in your Java programs How to create methods that ignore an exception, leaving it for another class to handle How to create your own exceptions
SampleProgram (1)
-- Whats the output of the following program?
public class SampleProgram { public static void main(String args[]) { String[] greek = {"Alpha", "Beta", "Gamma"}; System.out.println(greek[3]); } }
SampleProgram (2)
--The output
java.lang.ArrayIndexOutOfBoundsException at SampleProgram.main(SampleProgram.java:5) Exception in thread "main"
SampleProgram (3)
--Notes
Compiles successfully but encounters a problem when it runs The Java interpreter made note of the exception by displaying the error message and stopped the program. An object of type ArrayIndexOutOfBoundException is created to alert the user you have used an array element that isnt within the arrays boundaries. ArrayIndexOutOfBoundException is a subclass of Exception
SumNumbers (1)
public class SumNumbers { public static void main(String[] arguments) { float sum = 0; for (int i=0; i<arguments.length; i++) sum += Float.parseFloat(arguments[i]); System.out.println("Those numbers add up to "+ sum); } }
SumNumbers (2)
java SumNumbers 8 6 7 5 3 0 9 Output: Those numbers add up to 38.0
Java SumNumbers 1 3 5x Output?
SumNumbers (3)
java.lang.NumberFormatException: 5x at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1176) at java.lang.Float.parseFloat(Float.java:183) at SumNumbers.main(SumNumbers.java:7) Exception in thread "main"
NewSumNumbers (1)
public class NewSumNumbers { public static void main(String[] arguments) { float sum = 0; for (int i=0; i<arguments.length; i++) try { sum += Float.parseFloat(arguments[i]); } catch (NumberFormatException e) { System.out.println(arguments[i] + " is not a number."); }
System.out.println("Those numbers add up to "+ sum); }
NewSumNumbers (2)
Java NewSumNumbers 1 3 5x
Output: 5x is not a number. Those numbers add up to 4.0
NewSumNumbers (3)
A try-catch block can be used with any exception that you want a program to handle
try{ //statement(s) that might cause the exception } catch (Exception e) { //what to do when the exception occurs }
public class DivideNumbers { public static void main(String[] arguments) { if (arguments.length == 2) { int result =0; try { result = Integer.parseInt(arguments[0])/Integer.parseInt(arguments[1]); System.out.println(arguments[0] + " divided by " + arguments[1] + " equals " + result); } catch (NumberFormatException e) { System.out.println("Both arguments must be numbers."); } catch (ArithmeticException e) { System.out.println("You cannot divide by zero"); } } } }
try-catch-finally block
try{ //statement(s) that might cause the exception } catch (Exception e) { //what to do when the exception occurs } finally { //statements to execute no matter what }
Throwing Exceptions
sum += Float.parseFloat(arguments[i]);
} catch (NumberFormatException e) { System.out.println(arguments[i] + " is not a number."); } System.out.println("Those numbers add up to "+ sum); } }
sum += Float.parseFloat(arguments[i]);
} catch (NumberFormatException e) { System.out.println(arguments[i] + " is not a number."); throw e; } System.out.println("Those numbers add up to "+ sum); } }
getMessage() method
return the error message string (the argument of the 2nd constructor)
Declaring an Exception
If a method can throw an exception, you can always declare the type of the exception in the header after the keyword throws
public static double average( double [] dArray ) throws IllegalArgumentException
The compiler requires you to declare the possible exception throw if the exception class is not derived from RuntimeException or Error. These are called checked exceptions (checked by the compiler). Exceptions derived from RuntimeException are called unchecked exceptions and their declaration is optional.
Throwing an Exception
-- An Example
public static double average( double [] dArray ) throws IllegalArgumentException { if ( dArray.length == 0 ) throw new IllegalArgumentException(); else { double sum = 0.0; for ( int i = 0; i < dArray.length; i++ ) sum += dArray[ i ]; return sum / dArray.length; } }
27
Catastrophic failures: can not be prevented, but certain systems need to design in mechanisms to minimize the damage that they cause Some failures: can be anticipated and avoided through simple checks and guards. Other failures: must be handled as they arise, often use Javas exception handling mechanism.
The Output
Oops you cant add the arrays Wrong!