Java Exception Handling Dealing with errors using Javas

  • Slides: 45
Download presentation
Java Exception Handling Dealing with errors using Java’s exception handling mechanism Exception handling in

Java Exception Handling Dealing with errors using Java’s exception handling mechanism Exception handling in Java James Tam

Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java’s

Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java’s exception handling mechanism Exception handling in Java James Tam

Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java’s

Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java’s exception handling mechanism Exception handling in Java James Tam

Class Inventory: An Earlier Example class Inventory { public boolean add. To. Inventory (int

Class Inventory: An Earlier Example class Inventory { public boolean add. To. Inventory (int amount) { int temp = stock. Level + amount; if (temp > MAX) { System. out. print("Adding " + amount + " item will cause stock "); System. out. println("to become greater than " + MAX + " units"); return false; } else { stock. Level = stock. Level + amount; return true; } } : Exception handling in Java James Tam

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method 2() == false) return false; object 2. method 2 () If (store. add. To. Inventory(amt) == false) return false; store. add. To. Inventory (int amt) If (temp > MAX) return false; Exception handling in Java James Tam

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method 2() == false) return false; Problem 1: The calling method may forget to check the return value object 2. method 2 () If (store. add. To. Inventory(amt) == false) return false; store. add. To. Inventory (int amt) If (temp > MAX) return false; Exception handling in Java James Tam

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method 2() == false) return false; object 2. method 2 () If (store. add. To. Inventory(amt) == false) return false; store. add. To. Inventory (int amt) Problem 2: A long series of method calls requires many checks/returns Exception handling in Java If (temp > MAX) return false; James Tam

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method

Some Hypothetical Method Calls: Condition/Return object 1. method 1 () If (object 2. method 2() == false) return false; object 2. method 2 () If (store. add. To. Inventory(amt) == false) ? ? ? Problem 3: The calling method may not know how to handle the error Exception handling in Java return false; ? ? ? store. add. To. Inventory (int amt) If (temp > MAX) return false; James Tam

Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java’s

Approaches For Dealing With Error Conditions Use conditional statements and return values Use Java’s exception handling mechanism Exception handling in Java James Tam

Handling Exceptions Format: try { // Code that may cause an exception to occur

Handling Exceptions Format: try { // Code that may cause an exception to occur } catch (Exception. Type identifier) { // Code to handle the exception } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited The complete program can be found in the directory:

Handling Exceptions: An Example Revisited The complete program can be found in the directory: /home/profs/tamj/233/examples/exceptions/handling. Exceptions/first. Example class Simple. IO { public static void main (String [] argv) { : try { fw = new File. Writer (filename); : } catch (IOException e) { : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited File. Writer (String filename) { : Simple. IO. main

Handling Exceptions: An Example Revisited File. Writer (String filename) { : Simple. IO. main () } try { fw = new File. Writer (filename); } catch (IOException e) { : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited File. Writer (String filename) { Simple. IO. main ()

Handling Exceptions: An Example Revisited File. Writer (String filename) { Simple. IO. main () } Oops! Can’t write to file try { fw = new File. Writer (filename); } catch (IOException e) { : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited File. Writer (String filename) { IOException thrown IOException e=

Handling Exceptions: An Example Revisited File. Writer (String filename) { IOException thrown IOException e= new IOException () Simple. IO. main () } try { fw = new File. Writer (filename); } catch (IOException e) { : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited File. Writer (String filename) { IOException thrown IOException e=

Handling Exceptions: An Example Revisited File. Writer (String filename) { IOException thrown IOException e= new IOException () Simple. IO. main () } try { fw = new File. Writer (filename); } catch (IOException e) { : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited File. Writer (String filename) { Simple. IO. main ()

Handling Exceptions: An Example Revisited File. Writer (String filename) { Simple. IO. main () } try { fw = new File. Writer (filename); } catch (IOException e) { } IOException must be dealt with here Exception handling in Java James Tam

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw = new Print. Writer (fw); pw. println(iw 1. get. Num()); pw. close(); fr = new File. Reader(filename); br = new Buffered. Reader(fr); System. out. println("Read from file: " + br. read. Line()); } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw = new Print. Writer (fw); Exception 1 pw. println(iw 1. get. Num()); pw. close(); fr = new File. Reader(filename); br = new Buffered. Reader(fr); System. out. println("Read from file: " + br. read. Line()); } Exception handling in Java James Tam

Where The Exceptions Occur In Class File. Writer For online documentation for this class

Where The Exceptions Occur In Class File. Writer For online documentation for this class go to: http: //java. sun. com/j 2 se/1. 4. 1/docs/api/java/io/File. Writer. html Class File. Writer { public File. Writer (String file. Name) throws IOException; public File. Writer (String file. Name, boolean append) throws IOException; : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw = new Print. Writer (fw); pw. println(iw 1. get. Num()); pw. close(); fr = new File. Reader(filename); br = new Buffered. Reader(fr); Exception 2 System. out. println("Read from file: " + br. read. Line()); } Exception handling in Java James Tam

Where The Exceptions Occur In Class File. Reader For online documentation for this class

Where The Exceptions Occur In Class File. Reader For online documentation for this class go to: http: //java. sun. com/j 2 se/1. 4. 1/docs/api/java/io/File. Reader. html Class File. Reader { public File. Reader (String file. Name) throws File. Not. Found. Exception; public File. Reader (File file) throws File. Not. Found. Exception; : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw

Handling Exceptions: An Example Revisited try { fw = new File. Writer (filename); pw = new Print. Writer (fw); pw. println(iw 1. get. Num()); pw. close(); fr = new File. Reader(filename); br = new Buffered. Reader(fr); System. out. println("Read from file: " + br. read. Line()); } Exception 3 Exception handling in Java James Tam

Where The Exceptions Occur In Class Buffered. Reader For online documentation for this class

Where The Exceptions Occur In Class Buffered. Reader For online documentation for this class go to: http: //java. sun. com/j 2 se/1. 4. 1/docs/api/java/io/Buffered. Reader. html Class Buffered. Reader { public Buffered. Reader (Reader in); public Buffered. Reader (Reader in, int sz); public String read. Line () throws IOException; : } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited catch (IOException e) { System. out. println("File IO error:

Handling Exceptions: An Example Revisited catch (IOException e) { System. out. println("File IO error: Exception thrown"); System. out. println(e); System. out. println(); e. print. Stack. Trace(); } Exception handling in Java James Tam

Handling Exceptions: An Example Revisited java. io. File. Not. Found. Exception: data catch (IOException

Handling Exceptions: An Example Revisited java. io. File. Not. Found. Exception: data catch (IOException e) (No such file or directory) { System. out. println("File IO error: Exception thrown"); System. out. println(e); System. out. println(); e. print. Stack. Trace(); } java. io. File. Not. Found. Exception: data (No such file or directory) at java. io. File. Input. Stream. open(Native Method) at java. io. File. Input. Stream. <init>(File. Input. Stream. java: 103) at java. io. File. Input. Stream. <init>(File. Input. Stream. java: 66) at java. io. File. Reader. <init>(File. Reader. java: 41) at Simple. IO. main(Simple. IO. java: 35) Exception handling in Java James Tam

Common Exceptions Null. Pointer. Exception Array. Index. Out. Of. Bounds. Exception Arithmetic. Exception handling

Common Exceptions Null. Pointer. Exception Array. Index. Out. Of. Bounds. Exception Arithmetic. Exception handling in Java James Tam

Common Exceptions: An Example int [] arr = null; arr[0] = 1; Null. Pointer.

Common Exceptions: An Example int [] arr = null; arr[0] = 1; Null. Pointer. Exception arr = new int [4]; int i; for (i = 0; i <= 4; i++) arr[i] = i; arr[i-1] = arr[i-1] / 0; Exception handling in Java James Tam

Common Exceptions: An Example int [] arr = null; arr[0] = 1; arr =

Common Exceptions: An Example int [] arr = null; arr[0] = 1; arr = new int [4]; int i; for (i = 0; i <= 4; i++) arr[i] = i; Array. Index. Out. Of. Bounds. Exception (when i = 4) arr[i-1] = arr[i-1] / 0; Exception handling in Java James Tam

Common Exceptions: An Example int [] arr = null; arr[0] = 1; arr =

Common Exceptions: An Example int [] arr = null; arr[0] = 1; arr = new int [4]; int i; for (i = 0; i <= 4; i++) arr[i] = i; arr[i-1] = arr[i-1] / 0; Arithmetic. Exception (Division by zero) Exception handling in Java James Tam

Categories Of Exceptions Unchecked exceptions Checked exception Exception handling in Java James Tam

Categories Of Exceptions Unchecked exceptions Checked exception Exception handling in Java James Tam

Unchecked Exceptions • The compiler doesn’t require you to handle them if they are

Unchecked Exceptions • The compiler doesn’t require you to handle them if they are thrown. • They can occur at any time in the program (not just for a specific method) • Typically they are fatal runtime errors that are beyond your control • Use conditional statements rather than the exception handling model. • Examples: Null. Pointer. Exception, Index. Out. Of. Bounds. Exception, Arithmetic. Exception… Exception handling in Java James Tam

Checked Exceptions Must be handled if they are ever thrown • Use a try-catch

Checked Exceptions Must be handled if they are ever thrown • Use a try-catch block Deal with problems that occur in a specific place • When a particular method invoked Example: IOException handling in Java James Tam

Avoid Squelching Your Exceptions try { fw = new File. Writer (filename); } catch

Avoid Squelching Your Exceptions try { fw = new File. Writer (filename); } catch (IOException e) { // Do nothing here. Just set up the try-catch block to bypass those pesky // syntax errors. } Exception handling in Java James Tam

Avoid Squelching Your Exceptions try { NO! fw = new File. Writer (filename); }

Avoid Squelching Your Exceptions try { NO! fw = new File. Writer (filename); } catch (IOException e) { // Do nothing here. Just set up the try-catch block to bypass those pesky // syntax errors. } Exception handling in Java James Tam

The Finally Clause Part of Java’s exception handling model (try-catch-finally) Used to enclose statements

The Finally Clause Part of Java’s exception handling model (try-catch-finally) Used to enclose statements that must always be executed. Exception handling in Java James Tam

The Finally Clause try { } catch { } finally { } Exception handling

The Finally Clause try { } catch { } finally { } Exception handling in Java James Tam

The Finally Clause: Exception Thrown try { } catch 1) Statements in the try

The Finally Clause: Exception Thrown try { } catch 1) Statements in the try block are attempted 2) Exception is thrown and caught { } finally { 3) Control transfers to the finally clause } Exception handling in Java James Tam

The Finally Clause: No Exception Occurs try { } 1) Statements in the try

The Finally Clause: No Exception Occurs try { } 1) Statements in the try block are completed catch { } 2) Control transfers to the finally clause finally { } Exception handling in Java James Tam

Try-Catch-Finally: An Example The complete program can be found in the directory: /home/profs/tamj/233/examples/exceptions/handling. Exceptions/second.

Try-Catch-Finally: An Example The complete program can be found in the directory: /home/profs/tamj/233/examples/exceptions/handling. Exceptions/second. Example Exception handling in Java James Tam

Try-Catch-Finally: An Example (2) try { Buffered. Reader br = new Buffered. Reader(new File.

Try-Catch-Finally: An Example (2) try { Buffered. Reader br = new Buffered. Reader(new File. Reader("phil")); String s = br. read. Line(); while (s != null) s = br. read. Line(); return; } catch (IOException e) { e. print. Stack. Trace(); return; } finally { System. out. println("<<<Finished reading>>>"); return; } Exception handling in Java James Tam

When The Caller Can’t Handle The Exceptions method 2 () Exception thrown! method 1

When The Caller Can’t Handle The Exceptions method 2 () Exception thrown! method 1 () ? ? ? main () Exception handling in Java James Tam

When The Caller Can’t Handle The Exceptions: An Example The complete program can be

When The Caller Can’t Handle The Exceptions: An Example The complete program can be found in the directory: /home/profs/tamj/233/examples/exceptions/handling. Exceptions/third. Example import java. io. *; class Intermediate. IO { public static void main (String [] argv) { method 1 (); } Exception handling in Java James Tam

When The Caller Can’t Handle The Exceptions: An Example (2) public static void method

When The Caller Can’t Handle The Exceptions: An Example (2) public static void method 1 () { try { method 2 (); return; } catch (IOException e) { System. out. println("IOException thrown while reading input file"); e. print. Stack. Trace(); return; } } Exception handling in Java James Tam

When The Caller Can’t Handle The Exceptions: An Example (3) public static void method

When The Caller Can’t Handle The Exceptions: An Example (3) public static void method 2 () throws IOException { Buffered. Reader br = null; String s; br = new Buffered. Reader(new File. Reader("phil")); s = br. read. Line(); while (s != null) { System. out. println(s); s = br. read. Line(); } return; } Exception handling in Java James Tam

Summary Handling exceptions with the try-catch block Checked vs. unchecked exceptions Using the finally

Summary Handling exceptions with the try-catch block Checked vs. unchecked exceptions Using the finally clause to guarantee the execution of cleanup statements regardless of whether an exception occurs or not. Exception handling in Java James Tam