Java Exception Handling errors using Javas exception handling
![Java Exception Handling errors using Java’s exception handling mechanism James Tam Java Exception Handling errors using Java’s exception handling mechanism James Tam](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-1.jpg)
![Approaches For Dealing With Error Conditions • Use branches/decision making and return values • Approaches For Dealing With Error Conditions • Use branches/decision making and return values •](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-2.jpg)
![Class Inventory: An Earlier Example public class Inventory { public final int MIN = Class Inventory: An Earlier Example public class Inventory { public final int MIN =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-3.jpg)
![Class Inventory: An Earlier Example (2) else { stock. Level = stock. Level + Class Inventory: An Earlier Example (2) else { stock. Level = stock. Level +](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-4.jpg)
![Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2() Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2()](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-5.jpg)
![Some Hypothetical Method Calls: Condition/Return reference 1. method 1() Problem 1: The calling method Some Hypothetical Method Calls: Condition/Return reference 1. method 1() Problem 1: The calling method](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-6.jpg)
![Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2() Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2()](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-7.jpg)
![Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2() Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2()](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-8.jpg)
![Approaches For Dealing With Error Conditions • Use branches/decision making constructs and return values Approaches For Dealing With Error Conditions • Use branches/decision making constructs and return values](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-9.jpg)
![Handling Exceptions Format: try { // Code that may cause an error/exception to occur Handling Exceptions Format: try { // Code that may cause an error/exception to occur](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-10.jpg)
![Handling Exceptions: Reading Input Location of the online example: /home/233/examples/exceptions/handling. Exceptions/input. Example public class Handling Exceptions: Reading Input Location of the online example: /home/233/examples/exceptions/handling. Exceptions/input. Example public class](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-11.jpg)
![Handling Exceptions: Reading Input (2) try { System. out. print("Type an integer: "); s Handling Exceptions: Reading Input (2) try { System. out. print("Type an integer: "); s](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-12.jpg)
![Handling Exceptions: Where The Exceptions Occur The first exception can occur here try { Handling Exceptions: Where The Exceptions Occur The first exception can occur here try {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-13.jpg)
![Handling Exceptions: Result Of Calling Buffered. Reader. Read. Line() try { System. out. print("Type Handling Exceptions: Result Of Calling Buffered. Reader. Read. Line() try { System. out. print("Type](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-14.jpg)
![Where The Exceptions Occur In Class Buffered. Reader • For online documentation for this Where The Exceptions Occur In Class Buffered. Reader • For online documentation for this](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-15.jpg)
![Handling Exceptions: Result Of Calling Integer. Parse. Int () The second exception can occur Handling Exceptions: Result Of Calling Integer. Parse. Int () The second exception can occur](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-16.jpg)
![Where The Exceptions Occur In Class Integer • For online documentation for this class Where The Exceptions Occur In Class Integer • For online documentation for this class](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-17.jpg)
![Handling Exceptions: The Details try { System. out. print("Type an integer: "); s = Handling Exceptions: The Details try { System. out. print("Type an integer: "); s =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-18.jpg)
![Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Driver. main () try Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Driver. main () try](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-19.jpg)
![Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Oops! The user didn’t Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Oops! The user didn’t](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-20.jpg)
![Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-21.jpg)
![Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-22.jpg)
![Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-23.jpg)
![Handling Exceptions: Catching The Exception catch (Number. Format. Exception e) {. . . } Handling Exceptions: Catching The Exception catch (Number. Format. Exception e) {. . . }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-24.jpg)
![Catching The Exception: Error Messages catch (Number. Format. Exception e) { System. out. println(“You Catching The Exception: Error Messages catch (Number. Format. Exception e) { System. out. println(“You](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-25.jpg)
![Catching The Exception: Error Messages For input string: "james tam" catch (Number. Format. Exception Catching The Exception: Error Messages For input string: "james tam" catch (Number. Format. Exception](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-26.jpg)
![Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num = Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-27.jpg)
![Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num = Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-28.jpg)
![The Finally Clause • An additional part of Java’s exception handling model (trycatch-finally). • The Finally Clause • An additional part of Java’s exception handling model (trycatch-finally). •](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-29.jpg)
![The Finally Clause: Exception Thrown f. method () { try { f. method(); } The Finally Clause: Exception Thrown f. method () { try { f. method(); }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-30.jpg)
![The Finally Clause: Exception Thrown f. method () te the at u c e The Finally Clause: Exception Thrown f. method () te the at u c e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-31.jpg)
![The Finally Clause: No Exception Thrown f. method () try { f. method(); ecute The Finally Clause: No Exception Thrown f. method () try { f. method(); ecute](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-32.jpg)
![Try-Catch-Finally: An Example Location of the online example: /home/233/examples/exceptions/handling. Exceptions/try. Catch. Finally. Example public Try-Catch-Finally: An Example Location of the online example: /home/233/examples/exceptions/handling. Exceptions/try. Catch. Finally. Example public](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-33.jpg)
![Try-Catch-Finally: An Example (2) public class TCFExample { public void method () { Buffered. Try-Catch-Finally: An Example (2) public class TCFExample { public void method () { Buffered.](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-34.jpg)
![Try-Catch-Finally: An Example (3) catch (IOException e) { e. print. Stack. Trace(); return(); } Try-Catch-Finally: An Example (3) catch (IOException e) { e. print. Stack. Trace(); return(); }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-35.jpg)
![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](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-36.jpg)
![When The Caller Can’t Handle The Exceptions: An Example Location of the online example: When The Caller Can’t Handle The Exceptions: An Example Location of the online example:](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-37.jpg)
![When The Caller Can’t Handle The Exceptions: An Example (2) • Tracing the method When The Caller Can’t Handle The Exceptions: An Example (2) • Tracing the method](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-38.jpg)
![When The Caller Can’t Handle The Exceptions: An Example (3) • Tracing the method When The Caller Can’t Handle The Exceptions: An Example (3) • Tracing the method](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-39.jpg)
![When The Caller Can’t Handle The Exceptions: An Example (4) public class Driver { When The Caller Can’t Handle The Exceptions: An Example (4) public class Driver {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-40.jpg)
![When The Caller Can’t Handle The Exceptions: An Example (5) do { } } When The Caller Can’t Handle The Exceptions: An Example (5) do { } }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-41.jpg)
![When The Caller Can’t Handle The Exceptions: An Example (6) public class TCExample { When The Caller Can’t Handle The Exceptions: An Example (6) public class TCExample {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-42.jpg)
![When The Driver. Main () Method Can’t Handle The Exception public class Driver { When The Driver. Main () Method Can’t Handle The Exception public class Driver {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-43.jpg)
![After This Section You Should Now Know • The benefits of handling errors with After This Section You Should Now Know • The benefits of handling errors with](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-44.jpg)
![Simple File Input And Output You will learn how to write to and read Simple File Input And Output You will learn how to write to and read](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-45.jpg)
![Inheritance Hierarchy For IOExceptions IOException These classes are more specific instances of class IOException Inheritance Hierarchy For IOExceptions IOException These classes are more specific instances of class IOException](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-46.jpg)
![Inheritance And Catching Exceptions • If you are catching a sequence of exceptions then Inheritance And Catching Exceptions • If you are catching a sequence of exceptions then](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-47.jpg)
![Branches: Specific Before General • Correct If (x > 100) body; else if (x Branches: Specific Before General • Correct If (x > 100) body; else if (x](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-48.jpg)
![Inheritance And Catching Exceptions (2) Correct Incorrect try { { } } catch (EOFException Inheritance And Catching Exceptions (2) Correct Incorrect try { { } } catch (EOFException](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-49.jpg)
![Reading Text Input From A File Buffered. Reader 01000001 am 01000001 File. Reader ‘‘ Reading Text Input From A File Buffered. Reader 01000001 am 01000001 File. Reader ‘‘](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-50.jpg)
![Writing Text Output To A File Primitives, “AN “ 000 01 Objects 1 01001110 Writing Text Output To A File Primitives, “AN “ 000 01 Objects 1 01001110](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-51.jpg)
![File Input And Output: One Complete Example Location of the online example: /home/233/examples/file. IO/Driver. File Input And Output: One Complete Example Location of the online example: /home/233/examples/file. IO/Driver.](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-52.jpg)
![File IO: Get Data And Write To File // Get paragraph information from the File IO: Get Data And Write To File // Get paragraph information from the](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-53.jpg)
![File IO: Read Data From File try { fr = new File. Reader("data. txt"); File IO: Read Data From File try { fr = new File. Reader("data. txt");](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-54.jpg)
![You Should Now Know • How to write to files with Java classes • You Should Now Know • How to write to files with Java classes •](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-55.jpg)
- Slides: 55
![Java Exception Handling errors using Javas exception handling mechanism James Tam Java Exception Handling errors using Java’s exception handling mechanism James Tam](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-1.jpg)
Java Exception Handling errors using Java’s exception handling mechanism James Tam
![Approaches For Dealing With Error Conditions Use branchesdecision making and return values Approaches For Dealing With Error Conditions • Use branches/decision making and return values •](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-2.jpg)
Approaches For Dealing With Error Conditions • Use branches/decision making and return values • Use Java’s exception handling mechanism James Tam
![Class Inventory An Earlier Example public class Inventory public final int MIN Class Inventory: An Earlier Example public class Inventory { public final int MIN =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-3.jpg)
Class Inventory: An Earlier Example public class Inventory { public final int MIN = 0; public final int MAX = 100; public final int CRITICAL = 10; public boolean add(int amount) { int temp; 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 (overstock)"); return(false); } James Tam
![Class Inventory An Earlier Example 2 else stock Level stock Level Class Inventory: An Earlier Example (2) else { stock. Level = stock. Level +](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-4.jpg)
Class Inventory: An Earlier Example (2) else { stock. Level = stock. Level + amount; return(true); } } // End of method add(). . . James Tam
![Some Hypothetical Method Calls ConditionReturn reference 1 method 1 if reference 2 method 2 Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2()](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-5.jpg)
Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2() == false) return(false); reference 2. method 2() if (store. add. To. Inventory(amt) == false) return(false); store. add. To. Inventory(int amt) if (temp > MAX) return(false); James Tam
![Some Hypothetical Method Calls ConditionReturn reference 1 method 1 Problem 1 The calling method Some Hypothetical Method Calls: Condition/Return reference 1. method 1() Problem 1: The calling method](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-6.jpg)
Some Hypothetical Method Calls: Condition/Return reference 1. method 1() Problem 1: The calling method may forget to check the return value if (reference 2. method 2() == false) return(false); reference 2. method 2() if (store. add. To. Inventory(amt) == false) return(false); store. add. To. Inventory(int amt) if (temp > MAX) return(false); James Tam
![Some Hypothetical Method Calls ConditionReturn reference 1 method 1 if reference 2 method 2 Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2()](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-7.jpg)
Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2() == false) return(false); reference 2. method 2() if (store. add. To. Inventory(amt) == false) return(false); Problem 2: A long series of method calls requires many checks/returns store. add. To. Inventory(int amt) if (temp > MAX) return(false); James Tam
![Some Hypothetical Method Calls ConditionReturn reference 1 method 1 if reference 2 method 2 Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2()](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-8.jpg)
Some Hypothetical Method Calls: Condition/Return reference 1. method 1() if (reference 2. method 2() == false) return(false); reference 2. method 2() if (store. add. To. Inventory(amt) == false) ? ? return(false); ? ? Problem 3: The calling method may not know how to handle the error store. add. To. Inventory(int amt) if (temp > MAX) return(false); James Tam
![Approaches For Dealing With Error Conditions Use branchesdecision making constructs and return values Approaches For Dealing With Error Conditions • Use branches/decision making constructs and return values](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-9.jpg)
Approaches For Dealing With Error Conditions • Use branches/decision making constructs and return values • Use Java’s exception handling mechanism James Tam
![Handling Exceptions Format try Code that may cause an errorexception to occur Handling Exceptions Format: try { // Code that may cause an error/exception to occur](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-10.jpg)
Handling Exceptions Format: try { // Code that may cause an error/exception to occur } catch (Exception. Type identifier) { // Code to handle the exception } James Tam
![Handling Exceptions Reading Input Location of the online example home233examplesexceptionshandling Exceptionsinput Example public class Handling Exceptions: Reading Input Location of the online example: /home/233/examples/exceptions/handling. Exceptions/input. Example public class](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-11.jpg)
Handling Exceptions: Reading Input Location of the online example: /home/233/examples/exceptions/handling. Exceptions/input. Example public class Driver { public static void main (String [] args) { Buffered. Reader string. Input; Input. Stream. Reader character. Input; String s; int num; character. Input = new Input. Stream. Reader(System. in); string. Input = new Buffered. Reader(character. Input); James Tam
![Handling Exceptions Reading Input 2 try System out printType an integer s Handling Exceptions: Reading Input (2) try { System. out. print("Type an integer: "); s](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-12.jpg)
Handling Exceptions: Reading Input (2) try { System. out. print("Type an integer: "); s = string. Input. read. Line(); System. out. println("You typed in. . . " + s); num = Integer. parse. Int (s); System. out. println("Converted to an integer. . . " + num); } catch (IOException e) { System. out. println(e); } catch (Number. Format. Exception e) {. . . } } } James Tam
![Handling Exceptions Where The Exceptions Occur The first exception can occur here try Handling Exceptions: Where The Exceptions Occur The first exception can occur here try {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-13.jpg)
Handling Exceptions: Where The Exceptions Occur The first exception can occur here try { System. out. print("Type an integer: "); s = string. Input. read. Line(); System. out. println("You typed in. . . " + s); num = Integer. parse. Int (s); System. out. println("Converted to an integer. . . " +num); } James Tam
![Handling Exceptions Result Of Calling Buffered Reader Read Line try System out printType Handling Exceptions: Result Of Calling Buffered. Reader. Read. Line() try { System. out. print("Type](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-14.jpg)
Handling Exceptions: Result Of Calling Buffered. Reader. Read. Line() try { System. out. print("Type an integer: "); s = string. Input. read. Line(); System. out. println("You typed in. . . " + s); num = Integer. parse. Int (s); System. out. println("Converted to an integer. . . " +num); } James Tam
![Where The Exceptions Occur In Class Buffered Reader For online documentation for this Where The Exceptions Occur In Class Buffered. Reader • For online documentation for this](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-15.jpg)
Where The Exceptions Occur In Class Buffered. Reader • For online documentation for this class go to: - http: //docs. oracle. com/javase/7/docs/api/java/io/Buffered. Reader. htm l public class Buffered. Reader { public Buffered. Reader(Reader in); public Buffered. Reader(Reader in, int sz); public String read. Line() throws IOException; . . . } James Tam
![Handling Exceptions Result Of Calling Integer Parse Int The second exception can occur Handling Exceptions: Result Of Calling Integer. Parse. Int () The second exception can occur](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-16.jpg)
Handling Exceptions: Result Of Calling Integer. Parse. Int () The second exception can occur here try { System. out. print("Type an integer: "); s = string. Input. read. Line(); System. out. println("You typed in. . . " + s); num = Integer. parse. Int (s); System. out. println("Converted to an integer. . . " +num); } James Tam
![Where The Exceptions Occur In Class Integer For online documentation for this class Where The Exceptions Occur In Class Integer • For online documentation for this class](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-17.jpg)
Where The Exceptions Occur In Class Integer • For online documentation for this class go to: - http: //docs. oracle. com/javase/7/docs/api/java/lang/Integer. html public class Integer { public Integer(int value); public Integer(String s) throws Number. Format. Exception; . . . public static int parse. Int(String s) throws Number. Format. Exception; . . . } James Tam
![Handling Exceptions The Details try System out printType an integer s Handling Exceptions: The Details try { System. out. print("Type an integer: "); s =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-18.jpg)
Handling Exceptions: The Details try { System. out. print("Type an integer: "); s = string. Input. read. Line(); System. out. println("You typed in. . . " + s); num = Integer. parse. Int (s); System. out. println("Converted to an integer. . . " +num); } catch (IOException e) { System. out. println(e); } catch (Number. Format. Exception e) {. . . } } } James Tam
![Handling Exceptions Tracing The Example Integer parse IntString s Driver main try Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Driver. main () try](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-19.jpg)
Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Driver. main () try } { num = Integer. parse. Int(s); } : catch (Number. Format. Exception e) { : } James Tam
![Handling Exceptions Tracing The Example Integer parse IntString s Oops The user didnt Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Oops! The user didn’t](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-20.jpg)
Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Oops! The user didn’t enter an integer Driver. main () try } { num = Integer. parse. Int(s); } : catch (Number. Format. Exception e) { : } James Tam
![Handling Exceptions Tracing The Example Integer parse IntString s Number Format Exception e Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-21.jpg)
Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e = new Number. Format. Exception (); Driver. main () try } { num = Integer. parse. Int(s); } : catch (Number. Format. Exception e) { : } James Tam
![Handling Exceptions Tracing The Example Integer parse IntString s Number Format Exception e Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-22.jpg)
Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e = new Number. Format. Exception (); Driver. main () try } { num = Integer. parse. Int(s); } : catch (Number. Format. Exception e) { : } James Tam
![Handling Exceptions Tracing The Example Integer parse IntString s Number Format Exception e Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-23.jpg)
Handling Exceptions: Tracing The Example Integer. parse. Int(String s) { Number. Format. Exception e = new Number. Format. Exception (); Driver. main () try } { num = Integer. parse. Int(s); } : catch (Number. Format. Exception e) { } Exception must be dealt with here James Tam
![Handling Exceptions Catching The Exception catch Number Format Exception e Handling Exceptions: Catching The Exception catch (Number. Format. Exception e) {. . . }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-24.jpg)
Handling Exceptions: Catching The Exception catch (Number. Format. Exception e) {. . . } } } James Tam
![Catching The Exception Error Messages catch Number Format Exception e System out printlnYou Catching The Exception: Error Messages catch (Number. Format. Exception e) { System. out. println(“You](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-25.jpg)
Catching The Exception: Error Messages catch (Number. Format. Exception e) { System. out. println(“You entered a non-integer value. ”); System. out. println(e. get. Message()); System. out. println(e); e. print. Stack. Trace(); } } } James Tam
![Catching The Exception Error Messages For input string james tam catch Number Format Exception Catching The Exception: Error Messages For input string: "james tam" catch (Number. Format. Exception](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-26.jpg)
Catching The Exception: Error Messages For input string: "james tam" catch (Number. Format. Exception e) { System. out. println(“You entered a non-integer value. ”); System. out. println(e. get. Message()); System. out. println(e); e. print. Stack. Trace(); } java. lang. Number. Format. Exception: For input string: "james tam" } } java. lang. Number. Format. Exception: For input string: "james tam" at java. lang. Number. Format. Exception. for. Input. String(Number. Format. Exception. java: 48) at java. lang. Integer. parse. Int(Integer. java: 426) at java. lang. Integer. parse. Int(Integer. java: 476) at Driver. main(Driver. java: 39) James Tam
![Avoid Squelching Your Exceptions try s string Input read Line num Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-27.jpg)
Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num = Integer. parse. Int (s); } catch (IOException e) { System. out. println(e); } catch (Number. Format. Exception e) { // Do nothing here but set up the try-catch block to // bypass the “annoying” compiler error } James Tam
![Avoid Squelching Your Exceptions try s string Input read Line num Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num =](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-28.jpg)
Avoid Squelching Your Exceptions try { s = string. Input. read. Line(); num = Integer. parse. Int (s); } catch (IOException e) { System. out. println(e); } catch (Number. Format. Exception e) { // Do nothing here but set up the try-catch block to // bypass the “annoying” compiler error NO! } James Tam
![The Finally Clause An additional part of Javas exception handling model trycatchfinally The Finally Clause • An additional part of Java’s exception handling model (trycatch-finally). •](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-29.jpg)
The Finally Clause • An additional part of Java’s exception handling model (trycatch-finally). • Used to enclose statements that must always be executed whether or not an exception occurs. James Tam
![The Finally Clause Exception Thrown f method try f method The Finally Clause: Exception Thrown f. method () { try { f. method(); }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-30.jpg)
The Finally Clause: Exception Thrown f. method () { try { f. method(); } } catch { } finally { } James Tam
![The Finally Clause Exception Thrown f method te the at u c e The Finally Clause: Exception Thrown f. method () te the at u c e](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-31.jpg)
The Finally Clause: Exception Thrown f. method () te the at u c e x pt to e try block th m e t t A 1) in the ception d o h t me n ex a w o r may th try { f. method(); { 2) Exception thrown here } } catch { is n o pti ere e c x th 3) E augh c } finally 4) A the end of the catch block control transfers to the finally clause { } James Tam
![The Finally Clause No Exception Thrown f method try f method ecute The Finally Clause: No Exception Thrown f. method () try { f. method(); ecute](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-32.jpg)
The Finally Clause: No Exception Thrown f. method () try { f. method(); ecute x e o t mpt e 1) Atte ethod in th y the m at ma h t k c ption try blo e c x e an throw { 2) Code runs okay here } } catch { } () lly od fina h t me the. f f rs to o nd nsfe e a the ol tr A tr 3) con se u cla finally { } James Tam
![TryCatchFinally An Example Location of the online example home233examplesexceptionshandling Exceptionstry Catch Finally Example public Try-Catch-Finally: An Example Location of the online example: /home/233/examples/exceptions/handling. Exceptions/try. Catch. Finally. Example public](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-33.jpg)
Try-Catch-Finally: An Example Location of the online example: /home/233/examples/exceptions/handling. Exceptions/try. Catch. Finally. Example public class Driver { public static void main (String [] args) { TCFExample eg = new TCFExample (); eg. method(); } } James Tam
![TryCatchFinally An Example 2 public class TCFExample public void method Buffered Try-Catch-Finally: An Example (2) public class TCFExample { public void method () { Buffered.](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-34.jpg)
Try-Catch-Finally: An Example (2) public class TCFExample { public void method () { Buffered. Reader br; String s; int num; try { System. out. print("Type in an integer: "); br = new Buffered. Reader(new Input. Stream. Reader(System. in)); s = br. read. Line(); num = Integer. parse. Int(s); return; } James Tam
![TryCatchFinally An Example 3 catch IOException e e print Stack Trace return Try-Catch-Finally: An Example (3) catch (IOException e) { e. print. Stack. Trace(); return(); }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-35.jpg)
Try-Catch-Finally: An Example (3) catch (IOException e) { e. print. Stack. Trace(); return(); } catch (Number. Format. Exception e) { e. print. Stack. Trace (); return(); } finally { System. out. println("<<<This code will always execute>>>"); return; } } } James Tam
![When The Caller Cant Handle The Exceptions method 2 Exception thrown method 1 When The Caller Can’t Handle The Exceptions method 2 () Exception thrown! method 1](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-36.jpg)
When The Caller Can’t Handle The Exceptions method 2 () Exception thrown! method 1 () ? ? ? main () James Tam
![When The Caller Cant Handle The Exceptions An Example Location of the online example When The Caller Can’t Handle The Exceptions: An Example Location of the online example:](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-37.jpg)
When The Caller Can’t Handle The Exceptions: An Example Location of the online example: /home/233/examples/exceptions/handling. Exceptions/delegating. Exceptions James Tam
![When The Caller Cant Handle The Exceptions An Example 2 Tracing the method When The Caller Can’t Handle The Exceptions: An Example (2) • Tracing the method](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-38.jpg)
When The Caller Can’t Handle The Exceptions: An Example (2) • Tracing the method calls when no exception occurs: Driver. main() TCFExample. method() Buffered. Reader. read. Line() Integer. parse. Int() User enters 10 Break out of loop Yes indeed it is an integer! James Tam
![When The Caller Cant Handle The Exceptions An Example 3 Tracing the method When The Caller Can’t Handle The Exceptions: An Example (3) • Tracing the method](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-39.jpg)
When The Caller Can’t Handle The Exceptions: An Example (3) • Tracing the method calls when an exception does occur: Driver. main() TCFExample. method() Buffered. Reader. read. Line() Integer. parse. Int() User enters 1. 9 Return to the top of loop and start the calls again This string is not an integer. James Tam
![When The Caller Cant Handle The Exceptions An Example 4 public class Driver When The Caller Can’t Handle The Exceptions: An Example (4) public class Driver {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-40.jpg)
When The Caller Can’t Handle The Exceptions: An Example (4) public class Driver { public static void main (String [] args) { TCExample eg = new TCExample (); boolean input. Okay = true; James Tam
![When The Caller Cant Handle The Exceptions An Example 5 do When The Caller Can’t Handle The Exceptions: An Example (5) do { } }](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-41.jpg)
When The Caller Can’t Handle The Exceptions: An Example (5) do { } } try { eg. method(); input. Okay = true; } catch (IOException e) { e. print. Stack. Trace(); } catch (Number. Format. Exception e) { input. Okay = false; System. out. println("Please enter a whole number. "); } } while(input. Okay == false); // End of main // End of Driver class James Tam
![When The Caller Cant Handle The Exceptions An Example 6 public class TCExample When The Caller Can’t Handle The Exceptions: An Example (6) public class TCExample {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-42.jpg)
When The Caller Can’t Handle The Exceptions: An Example (6) public class TCExample { public void method () throws IOException, Number. Format. Exception { Buffered. Reader br; String s; int num; System. out. print("Type in an integer: "); br = new Buffered. Reader(new Input. Stream. Reader(System. in)); s = br. read. Line(); num = Integer. parse. Int(s); } } James Tam
![When The Driver Main Method Cant Handle The Exception public class Driver When The Driver. Main () Method Can’t Handle The Exception public class Driver {](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-43.jpg)
When The Driver. Main () Method Can’t Handle The Exception public class Driver { public static void main (String [] args) throws IOException, Number. Format. Exception { TCExample eg = new TCExample (); eg. method(); } } James Tam
![After This Section You Should Now Know The benefits of handling errors with After This Section You Should Now Know • The benefits of handling errors with](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-44.jpg)
After This Section You Should Now Know • The benefits of handling errors with an exception handler rather than employing a series of return values and conditional statements/branches. • How to handle exceptions - Being able to call a method that may throw an exception by using a trycatch block - What to do if the caller cannot properly handle the exception - What is the finally clause, how does it work and when should it be used • The effect of the inheritance hierarchy when catching exceptions James Tam
![Simple File Input And Output You will learn how to write to and read Simple File Input And Output You will learn how to write to and read](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-45.jpg)
Simple File Input And Output You will learn how to write to and read from text files in Java. James Tam
![Inheritance Hierarchy For IOExceptions IOException These classes are more specific instances of class IOException Inheritance Hierarchy For IOExceptions IOException These classes are more specific instances of class IOException](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-46.jpg)
Inheritance Hierarchy For IOExceptions IOException These classes are more specific instances of class IOException EOFException File. Not. Found Exception James Tam
![Inheritance And Catching Exceptions If you are catching a sequence of exceptions then Inheritance And Catching Exceptions • If you are catching a sequence of exceptions then](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-47.jpg)
Inheritance And Catching Exceptions • If you are catching a sequence of exceptions then make sure that you catch the exceptions for the child classes before you catch the exceptions for the parent classes • Deal with the more specific case before handling the more general case James Tam
![Branches Specific Before General Correct If x 100 body else if x Branches: Specific Before General • Correct If (x > 100) body; else if (x](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-48.jpg)
Branches: Specific Before General • Correct If (x > 100) body; else if (x > 10) body; else if (x > 0) body; • Incorrect If (x > 0) body; else if (x > 100) body; James Tam
![Inheritance And Catching Exceptions 2 Correct Incorrect try catch EOFException Inheritance And Catching Exceptions (2) Correct Incorrect try { { } } catch (EOFException](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-49.jpg)
Inheritance And Catching Exceptions (2) Correct Incorrect try { { } } catch (EOFException e) catch (IOException e) { { } } catch (IOException e) catch (EOFException e) { { } } James Tam
![Reading Text Input From A File Buffered Reader 01000001 am 01000001 File Reader Reading Text Input From A File Buffered. Reader 01000001 am 01000001 File. Reader ‘‘](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-50.jpg)
Reading Text Input From A File Buffered. Reader 01000001 am 01000001 File. Reader ‘‘ am re st : byte ‘N’ 01001110 ar 01000001 ch 01001110 ‘A’ 00100000 “AN “ String James Tam
![Writing Text Output To A File Primitives AN 000 01 Objects 1 01001110 Writing Text Output To A File Primitives, “AN “ 000 01 Objects 1 01001110](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-51.jpg)
Writing Text Output To A File Primitives, “AN “ 000 01 Objects 1 01001110 01000001 : st re am 01000001 te File. Writer File by am tre rs 00 ‘A’ 10 0 ‘N’ 00 0 ‘‘ cha 1 By objects we of course mean references to objects 01 00 11 10 Print. Writer 010 Strings, James Tam
![File Input And Output One Complete Example Location of the online example home233examplesfile IODriver File Input And Output: One Complete Example Location of the online example: /home/233/examples/file. IO/Driver.](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-52.jpg)
File Input And Output: One Complete Example Location of the online example: /home/233/examples/file. IO/Driver. java public class Driver { final static int MAX = 4; public static void main(String [] args) { String line = null; String [] paragraph = null; int i; Scanner in; // File IO Print. Writer pw = null; File. Writer fw = null; Buffered. Reader br = null; File. Reader fr = null; in = new Scanner(System. in); paragraph = new String[MAX]; James Tam
![File IO Get Data And Write To File Get paragraph information from the File IO: Get Data And Write To File // Get paragraph information from the](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-53.jpg)
File IO: Get Data And Write To File // Get paragraph information from the user. for (i = 0; i < MAX; i++) { System. out. print("Enter line of text: "); line = in. next. Line(); paragraph[i] = line; //Add line as array element } // Write paragraph to file try { fw = new File. Writer("data. txt"); // Open pw = new Print. Writer(fw); for (i = 0; i < MAX; i++) pw. println(paragraph[i]); fw. close(); // Close } catch (IOException e) { System. out. println("Error writing to file"); } James Tam
![File IO Read Data From File try fr new File Readerdata txt File IO: Read Data From File try { fr = new File. Reader("data. txt");](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-54.jpg)
File IO: Read Data From File try { fr = new File. Reader("data. txt"); br = new Buffered. Reader(fr); line = br. read. Line(); // Open if (line == null) System. out. println("Empty file, nothing to read"); while (line != null) { System. out. println(line); line = br. read. Line(); } fr. close(); // Close } catch (File. Not. Found. Exception e) { System. out. println("Could not open data. txt"); } catch (IOException e) { System. out. println("Trouble reading from data. txt"); } James Tam
![You Should Now Know How to write to files with Java classes You Should Now Know • How to write to files with Java classes •](https://slidetodoc.com/presentation_image_h/a6af85dfd630b7a00960858c2d1af677/image-55.jpg)
You Should Now Know • How to write to files with Java classes • File. Writer • Print. Writer • How to reading text information from files with Java classes • File. Reader • Buffered. Reader James Tam
Java gotchas
Exception handling in java
Exception handling pada java
Apa itu exception
Exception handling pl/sql
Vb net try
Throw exception php
No irq handler for vector
Event handling in ada
Php global exception handler
Vb net error handling
Thread class hierarchy in java
Robust programs
Exception vs error in java
Java exception hierarchy
Java exception error
Java unchecked exception
Java exception localized message
"unit4"
Gui event handling
Java swing form example
Import java.util.*
Import java.awt.*
How to import java.util.scanner
Java import java.util.*
Import java.util.*
Java.util.random
Import java.io.* in java
Import java.util.*;
Java thread import
Pengertian awt dan swing pada java
Import java.awt.event.*;
Java compiler translates java source code into
Javatpoint ejb
Payroll system using polymorphism in java
Leanft vs selenium
Rmi
Dining philosophers problem using monitors java
Bank account java program using inheritance
Bank account program in java using inheritance
Stacks in data structures
Moving ball program in java using applet
Data structures using java
Data structures using java
Java send email
Android boot camp for developers using java
Android boot camp for developers using java
Using system.collections
Unit 25 special refrigeration system components
System argument exception
Interface foo
Class a void foo() throws exception
Unt concur
What is microbiology
Runtimeexception extends exception
What is exception