cse 1341 principles of computer science i
DESCRIPTION
CSE 1341 Principles of Computer Science I. Spring 2008 Mark Fontenot [email protected]. Note Set 10. Note Set 10 Overview. Exception Handling. exception. Exception – an indication of a problem that occurs during a program’s execution - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/2.jpg)
Note Set 10 Overview
Exception Handling
![Page 3: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/3.jpg)
exceptionException – an indication of a problem that occurs during a
program’s executionException 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
![Page 4: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/4.jpg)
The standard
Do Something (processing)Check to see if it worked (error checking)
Do something elseCheck to see if it worked
Do SomethingCheck to see if it worked
![Page 5: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/5.jpg)
1 // Fig. 13.1: DivideByZeroNoExceptionHandling.java 2 // An application that attempts to divide by zero. 3 import java.util.Scanner; 4 5 public class DivideByZeroNoExceptionHandling 6 { 7 // demonstrates throwing an exception when a divide-by-zero occurs 8 public static int quotient( int numerator, int denominator ) 9 { 10 return numerator / denominator; // possible division by zero 11 } // end method quotient 12 13 public static void main( String args[] ) 14 { 15 Scanner scanner = new Scanner( System.in ); // scanner for input 16 17 System.out.print( "Please enter an integer numerator: " ); 18 int numerator = scanner.nextInt(); 19 System.out.print( "Please enter an integer denominator: " ); 20 int denominator = scanner.nextInt(); 21 22 int result = quotient( numerator, denominator ); 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
Attempt to divide; denominator may be zero
Read input; exception occurs if input is not a valid integer
![Page 6: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/6.jpg)
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
![Page 7: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/7.jpg)
Exception Handling Codetry { //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 }catch (Exception e) { //Can handle multiple types of exceptions}
Exceptionhandlers
Uncaught exception – exception for which there is no exception handler - exception type doesn’t match any of the catch blocks
Try Block
Exception Parameter
![Page 8: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/8.jpg)
ExceptionTestpublic class ExceptionTest { public static void main (String [] args) { int x = 3; int y = 0; int z = x / y; System.out.println(z); } }
throws ArithmeticException
run:Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTest.main(ExceptionTest.java:9)Java Result: 1
![Page 9: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/9.jpg)
ExceptionTest2public 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 Caught0
![Page 10: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/10.jpg)
With Scannerimport 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(); } } }
![Page 11: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/11.jpg)
Use Java API
JAVA API lists the exceptions thrown by each method
![Page 12: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/12.jpg)
Termination Model of Exception Handling
try { code line 1 code line 2 code line 3} catch (Exception e) { handler 1 handler 2} code line 4code line 5
if line 2 throws exception,control transfers to exception handler then to line of code afterhandlers.
![Page 13: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/13.jpg)
ObjectvilleAll exception classes directly or indirectly extend
java.lang.Exception.It is possible to create your own exception classes by
extending java.lang.Exception.
![Page 14: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/14.jpg)
ObjectvilleUnchecked Exceptions
Subclass of RuntimeExceptionNot required to be handled
Checked ExceptionsSubclass of ExceptionMust be handled or explicitly
or declared w/ throws
![Page 15: CSE 1341 Principles of Computer Science I](https://reader034.vdocuments.mx/reader034/viewer/2022051317/5681628c550346895dd2f92e/html5/thumbnails/15.jpg)
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
}