Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSE 1341 - Honors Principles of Computer Science I Spring 2008 Mark Fontenot [email protected] Note Set 16 Note Set 16 Overview Exception Handling exception Exception – an indication of a problem that occurs during a program’s execution Exception handling allows the graceful handling of errors in a program. Goal – allow the program to continue executing as if no problem had been encountered Can somewhat separate error handling code from regular processing code The standard Do Something (processing) Check to see if it worked (error checking) Do something else Check to see if it worked Do Something Check to see if it worked 1 // Fig. 13.1: DivideByZeroNoExceptionHandling.java 2 3 // An application that attempts to divide by zero. import java.util.Scanner; 4 5 6 public class DivideByZeroNoExceptionHandling { 7 8 // demonstrates throwing an exception when a divide-by-zero occurs public static int quotient( int numerator, int denominator ) 9 10 { 11 } // end method quotient Attempt to divide; denominator may be zero return numerator / denominator; // possible division by zero 12 13 14 15 16 17 public static void main( String args[] ) { Scanner scanner = new Scanner( System.in ); // scanner for input System.out.print( "Please enter an integer numerator: " ); 18 19 20 int numerator = scanner.nextInt(); System.out.print( "Please enter an integer denominator: " ); int denominator = scanner.nextInt(); 21 22 int result = quotient( numerator, denominator ); Read input; exception occurs if input is not a valid integer 23 System.out.printf( 24 "\nResult: %d / %d = %d\n", numerator, denominator, result ); 25 } // end main 26 } // end class DivideByZeroNoExceptionHandling Please enter an integer numerator: 100 Please enter an integer denominator: 7 Result: 100 / 7 = 14 Please enter an integer numerator: 100 Please enter an integer denominator: 7 Result: 100 / 7 = 14 Please enter an integer numerator: 100 Please enter an integer denominator: 0 Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideByZeroNoExceptionHandling.quotient(DivideByZeroNoExceptionHandling.java:10) at DivideByZeroNoExceptionHandling.main(DivideByZeroNoExceptionHandling.java:22) Please enter an integer numerator: 100 Please enter an integer denominator: hello Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at DivideByZeroNoExceptionHandling.main(DivideByZeroNoExceptionHandling.java:20) Stack Trace Exception Handling Code try { //put code here that might // throw an exception //if exception is thrown, move to catch } catch (InputMismatchException e) { //Handle the exception case here – do what is //needed to gracefully recover from the //exception } Exception Parameter catch (Exception e) { //Can handle multiple types of exceptions } Try Block Exception handlers Uncaught exception – exception for which there is no exception handler - exception type doesn’t match any of the catch blocks ExceptionTest public class ExceptionTest { public static void main (String [] args) { int x = 3; int y = 0; throws ArithmeticException int z = x / y; System.out.println(z); } } run: Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:9) Java Result: 1 ExceptionTest2 public class ExceptionTest2 { public static void main (String [] args) { int x = 3; int y = 0; int z = 0; try { z = x / y; } catch (ArithmeticException e) { System.out.println("Exception Caught"); } System.out.println(z); } } run-single: Exception Caught 0 With Scanner import java.util.*; public class ExceptionTest3 { public static void main (String [] args) { Scanner s = new Scanner(System.in); int x = 0; int y = 0; try { x = s.nextInt(); } catch (InputMismatchException e) { e.printStackTrace(); } } } Use Java API JAVA API lists the exceptions thrown by each method Termination Model of Exception Handling try { } code line 1 code line 2 code line 3 catch (Exception e) { handler 1 handler 2 } code line 4 code line 5 if line 2 throws exception, control transfers to exception handler then to line of code after handlers. Objectville All exception classes directly or indirectly extend java.lang.Exception. It is possible to create your own exception classes by extending java.lang.Exception. Objectville Unchecked Exceptions Subclass of RuntimeException Not required to be handled Checked Exceptions Subclass of Exception Must be handled or explicitly or declared w/ throws throws public int myFunction () throws MySpecialException { //Code here that could throw a MySpecialException //but you don’t want to handle it here for some reason //so you have to declare that this method could also throw //MySpecialException – a checked exception }