Java Exception Handling Dealing with errors using Javas
- Slides: 45
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 exception handling mechanism Exception handling in Java James Tam
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 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 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 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 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 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 exception handling mechanism Exception handling in Java James Tam
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: /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 () } 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 () } 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= 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= 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 () } 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 = 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 = 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 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 = 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 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 = 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 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: 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 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 in Java James Tam
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 = 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 = 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
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 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 (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); } 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 that must always be executed. Exception handling in Java James Tam
The Finally Clause try { } catch { } finally { } Exception handling in Java James Tam
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 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. Example Exception handling in Java James Tam
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 () ? ? ? main () Exception handling in Java James Tam
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 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 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 clause to guarantee the execution of cleanup statements regardless of whether an exception occurs or not. Exception handling in Java James Tam
- Java gotchas
- Exception handling in java
- Exception handling pada java
- Dasar exception
- Exception handling pl sql
- Exception handling in vb
- Exception handling in php example
- No irq handler for vector
- Event handling in ada
- Php custom exception
- Exception handling in vb net
- Types of exception in java
- Robustness in java
- Exception vs error in java
- Java exception hierarchy
- Java exception error
- Java unchecked exception
- Java exception localized message
- Java unchecked exception
- Java gui event handling
- Branch of linguistics dealing with meaning
- Abiotic factors examples
- Section 2 dealing with other nations
- Call center stress syndrome
- Dealing with competition marketing management
- Genre of speculative fiction
- Chapter 5 lesson 1 dealing with anxiety and depression
- Chapter 5 lesson 1 dealing with anxiety and depression
- Dealing with competition marketing management
- Unit 1 dealing with incoming calls
- Dealing with anger in the bible
- Dealing successfully with difficult changes in your life
- Areas of lexicology
- Referred to a play with an unhappy ending.
- Dealing with unstructured data
- Reuters electronic trading
- Dealing with unstructured data
- The branch of zoology dealing with insects
- The branch of zoology dealing with insects
- Dealing with challenging patients
- Dealing successfully with difficult changes
- Dealing with unstructured data
- Insider dealing
- Receive and resolve customer complaints
- 3p fair dealing
- Approach synoynm