James Tam Java Exception Handling Handling errors using Java’s exception handling mechanism.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
COMP 121 Week 5: Exceptions and Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
Java Exceptions. Exceptions Often in computing, operations cannot properly execute because some sort of error has occurred. Some examples: Often in computing,
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
MSc IT Programming Methodology (2). THROWS an EXCEPTION Errors?
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
James Tam Java Exception Handling Handling errors using Java’s exception handling mechanism.
James Tam CPSC 233: Introduction to Classes and Objects, Part I Attributes and methods Creating new classes References: Dynamic memory allocation and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
James Tam Java Exception Handling Handling errors using Java’s exception handling mechanism.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
Java Exception Handling ● Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions: – Examples:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
James Tam Object-Oriented Principles in Java: Part I Encapsulation Information Hiding Implementation Hiding Creating Objects in Java Class attributes.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Exception Handling Handling errors using Java’s exception handling mechanism.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
CS 2511 Fall  Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions:  Examples: Out.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exceptions By the end of this lecture you should be able to: explain the term exception; distinguish between checked and unchecked exception classes in.
Java Exception Handling Handling errors using Java’s exception handling mechanism.
1 Advanced Flow of Control : Introduction This chapter focuses on: –exception processing –catching and handling exceptions –creating new exceptions –exception.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
James Tam Java Exception Handling Handling errors using Java’s exception handling mechanism.
Java Exception Handling
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
CS1101: Programming Methodology Recitation 7 – Exceptions
Introduction to Exceptions in Java
CS102 – Exceptions David Davenport Latest: May 2015
Exception Handling and Reading / Writing Files
Exception Handling.
Web Design & Development Lecture 7
Exception Handling in Java
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Exception Handling Contents
Java Basics Exception Handling.
Java Exception Handling
Presentation transcript:

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

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

James Tam 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 addToInventory (int amount) { int temp; temp = stockLevel + 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 { stockLevel = stockLevel + amount; return true; } } // End of method addToInventory :

James Tam Some Hypothetical Method Calls: Condition/Return store.addToInventory (int amt) if (temp > MAX) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; reference1.method1 () if (reference2.method2() == false) return false;

James Tam Some Hypothetical Method Calls: Condition/Return store.addToInventory (int amt) if (temp > MAX) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; reference1.method1 () if (reference2.method2() == false) return false; Problem 1: The calling method may forget to check the return value

James Tam Some Hypothetical Method Calls: Condition/Return store.addToInventory (int amt) if (temp > MAX) return false; reference2.method2 () if (store.addToInventory(amt) == false) return false; reference1.method1 () if (reference2.method2() == false) return false; Problem 2: A long series of method calls requires many checks/returns

James Tam Some Hypothetical Method Calls: Condition/Return store.addToInventory (int amt) if (temp > MAX) return false; reference.method2 () if (store.addToInventory(amt) == false) return false; reference1.method1 () if (reference2.method2() == false) return false; Problem 3: The calling method may not know how to handle the error ??

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

James Tam Handling Exceptions Format: try { // Code that may cause an error/exception to occur } catch (ExceptionType identifier) { // Code to handle the exception }

James Tam Handling Exceptions: Reading Input The complete program can be found in the directory: /home/233/examples/exceptions/handlingExceptions/inputExample import java.io.*; class Driver { public static void main (String [] args) { BufferedReader stringInput; InputStreamReader characterInput; String s; int num; characterInput = new InputStreamReader(System.in); stringInput = new BufferedReader(characterInput);

James Tam Handling Exceptions: Reading Input (2) try { System.out.print("Type an integer: "); s = stringInput.readLine(); System.out.println("You typed in..." + s); num = Integer.parseInt (s); System.out.println("Converted to an integer..." + num); } catch (IOException e) { System.out.println(e); } catch (NumberFormatException e) { ::: }

James Tam Handling Exceptions: Where The Exceptions Occur try { System.out.print("Type an integer: "); s = stringInput.readLine(); System.out.println("You typed in..." + s); num = Integer.parseInt (s); System.out.println("Converted to an integer..." + num); }

James Tam Handling Exceptions: Result Of Calling ReadLine () try { System.out.print("Type an integer: "); s = stringInput.readLine(); System.out.println("You typed in..." + s); num = Integer.parseInt (s); System.out.println("Converted to an integer..." + num); } The first exception can occur here

James Tam Where The Exceptions Occur In Class BufferedReader For online documentation for this class go to: public class BufferedReader { public BufferedReader (Reader in); public BufferedReader (Reader in, int sz); public String readLine () throws IOException; : }

James Tam Handling Exceptions: Result Of Calling ParseInt () try { System.out.print("Type an integer: "); s = stringInput.readLine(); System.out.println("You typed in..." + s); num = Integer.parseInt (s); System.out.println("Converted to an integer..." + num); } The second exception can occur here

James Tam Where The Exceptions Occur In Class Integer For online documentation for this class go to: public class Integer { public Integer (int value); public Integer (String s) throws NumberFormatException;: public static int parseInt (String s) throws NumberFormatException;: }

James Tam Handling Exceptions: The Details try { System.out.print("Type an integer: "); s = stringInput.readLine(); System.out.println("You typed in..." + s); num = Integer.parseInt (s); System.out.println("Converted to an integer..." + num); } catch (IOException e) { System.out.println(e); } catch (NumberFormatException e) { :::::: } } }

James Tam Handling Exceptions: Tracing The Example Integer.parseInt (String s) { : } Driver.main () try { num = Integer.parseInt (s); } : catch (NumberFormatException e) { : }

James Tam Handling Exceptions: Tracing The Example Integer.parseInt (String s) { } Driver.main () try { num = Integer.parseInt (s); } : catch (NumberFormatException e) { : } Oops! The user didn’t enter an integer

James Tam Handling Exceptions: Tracing The Example Integer.parseInt (String s) { } Driver.main () try { num = Integer.parseInt (s); } : catch (NumberFormatException e) { : } NumberFormatException e = new NumberFormatException ();

James Tam Handling Exceptions: Tracing The Example Integer.parseInt (String s) { } Driver.main () try { num = Integer.parseInt (s); } : catch (NumberFormatException e) { : } NumberFormatException e = new NumberFormatException ();

James Tam Handling Exceptions: Tracing The Example Integer.parseInt (String s) { } Driver.main () try { num = Integer.parseInt (s); } : catch (NumberFormatException e) { } Exception must be dealt with here

James Tam Handling Exceptions: Catching The Exception catch (NumberFormatException e) { ::: }

James Tam Catching The Exception: Error Messages catch (NumberFormatException e) { System.out.println(e.getMessage()); System.out.println(e); e.printStackTrace(); }

James Tam Catching The Exception: Error Messages catch (NumberFormatException e) { System.out.println(e.getMessage()); System.out.println(e); e.printStackTrace(); } For input string: "james tam" java.lang.NumberFormatException: For input string: "james tam" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:426) at java.lang.Integer.parseInt(Integer.java:476) at Driver.main(Driver.java:39)

James Tam Categories Of Exceptions Unchecked exceptions Checked exception

James Tam Characteristics Of Unchecked Exceptions The compiler doesn’t require you to catch them if they are thrown. No try-catch block required by the compiler They can occur at any time in the program (not just for a specific method) Typically they are fatal runtime errors that are beyond the programmer’s control Use conditional statements rather than the exception handling model. Examples: NullPointerException,IndexOutOfBoundsException, ArithmeticException…

James Tam Common Unchecked Exceptions: NullPointerException 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; NullPointerException

James Tam Common Unchecked Exceptions: ArrayIndexOutOfBoundsException 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; ArrayIndexOutOfBoundsException (when i = 4)

James Tam Common Unchecked Exceptions: ArithmeticExceptions 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; ArithmeticException (Division by zero)

James Tam Checked Exceptions Must be handled if the potential for an error exists You must use a try-catch block Deal with problems that occur in a specific place When a particular method is invoked you must enclose it within a try- catch block Example: NumberFormatException, IOException

James Tam Avoid Squelching Your Exceptions try { s = stringInput.readLine(); num = Integer.parseInt (s); } catch (IOException e) { System.out.println(e); } catch (NumberFormatException 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 = stringInput.readLine(); num = Integer.parseInt (s); } catch (IOException e) { System.out.println(e); } catch (NumberFormatException 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 Java’s exception handling model (try- catch-finally). Used to enclose statements that must always be executed whether or not an exception occurs.

James Tam The Finally Clause: Exception Thrown try { f.method(); } catch { } finally { } Foo.method () { }

James Tam The Finally Clause: Exception Thrown try { f.method(); } catch { } finally { } 4) A the end of the catch block control transfers to the finally clause 1) Attempt to execute the method in the try block that may throw an exception f.method () { } 2) Exception thrown here 3) Exception is caught here

James Tam The Finally Clause: No Exception Thrown try { f.method(); } catch { } finally { } 1) Attempt to execute the method in the try block that may throw an exception f.method () { } 2) Code runs okay here 3) A the end of f.method () control transfers to the finally clause

James Tam Try-Catch-Finally: An Example The complete program can be found in the directory: /home/233/examples/exceptions/handlingExceptions/ tryCatchFinallyExample class Driver { public static void main (String [] args) { TCFExample eg = new TCFExample (); eg.method(); }

James Tam Try-Catch-Finally: An Example (2) public class TCFExample { public void method () { BufferedReader br; String s; int num; try { System.out.print("Type in an integer: "); br = new BufferedReader(new InputStreamReader(System.in)); s = br.readLine(); num = Integer.parseInt(s); return; }

James Tam Try-Catch-Finally: An Example (3) catch (IOException e) { e.printStackTrace(); return; } catch (NumberFormatException e) { e.printStackTrace (); return; } finally { System.out.println(" >>"); return; }

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

James Tam When The Caller Can’t Handle The Exceptions: An Example The complete program can be found in the directory: /home/233/examples/exceptions/handlingExceptions/ delegatingExceptions class Driver { public static void main (String [] args) { TCExample eg = new TCExample (); boolean inputOkay = true;

James Tam When The Caller Can’t Handle The Exceptions: An Example (2) do { try { eg.method(); inputOkay = true; } catch (IOException e) { e.printStackTrace(); } catch (NumberFormatException e) { inputOkay = false; System.out.println("Please enter a whole number."); } } while (inputOkay == false); }// End of main }// End of Driver class

James Tam When The Caller Can’t Handle The Exceptions: An Example (3) import java.io.*; public class TCExample { public void method () throws IOException, NumberFormatException { BufferedReader br; String s; int num; System.out.print("Type in an integer: "); br = new BufferedReader(new InputStreamReader(System.in)); s = br.readLine(); num = Integer.parseInt(s); }

James Tam When The Main () Method Can’t Handle The Exception class Driver { public static void main (String [] args) throws IOException, NumberFormatException { TCExample eg = new TCExample (); eg.method(); }

James Tam Creating Your Own Exceptions Throwable Error VirtualMachineError OutOfMemoryError Exception … IOExceptionRunTime Exception … ??? Excerpt from Big Java by C. Horstmann p. 562

James Tam Class Exception: The Local Inheritance Hierarchy Exception IOExceptionClassNotFound Exception CloneNotFound Exception EOFExceptionFileNotFound Exception MalformedURL Exception UnknownHost Exception

James Tam Writing New Exceptions: An Example The full example can be found in the directory: /home/233/examples/exceptions/writingExceptions/inventoryExample

James Tam Writing New Exceptions: The Driver Class class Driver { public static void main (String [] argv) { Inventory chinookInventory = new Inventory (); CommandProcessor userInterface = new CommandProcessor (chinookInventory); userInterface.startProcessingInput (); }

James Tam Writing New Exceptions: Class CommandProcessor public class CommandProcessor { private char menuOption; private Inventory storeInventory; public CommandProcessor (Inventory storeToTrack) { menuOption = 'q'; storeInventory = storeToTrack; }

James Tam Writing New Exceptions: Class CommandProcessor (2) public void startProcessingInput () { do { displayMenu(); readMenuOption(); switch (menuOption) { case 'a': case 'A': storeInventory.getAmountToAdd(); break; case 'r': case 'R': storeInventory.getAmountToRemove(); break;

James Tam Writing New Exceptions: Class CommandProcessor (3) case 'd': case 'D': storeInventory.displayInventoryLevel(); break; case 'c': case 'C': if (storeInventory.inventoryTooLow()) System.out.println("Stock levels critical!"); else System.out.println("Stock levels okay"); storeInventory.displayInventoryLevel(); break; case 'q': case 'Q': System.out.println("Quitting program"); break;

James Tam Writing New Exceptions: Class CommandProcessor (4) default: System.out.println("Enter one of A, R, D, C or Q"); } } while ((menuOption != 'Q') && (menuOption != 'q')); } // End of method startProcessingInput

James Tam Writing New Exceptions: Class CommandProcessor (5) protected void displayMenu () { System.out.println("\n\nINVENTORY PROGRAM: OPTIONS"); System.out.println("\t(A)dd new stock to inventory"); System.out.println("\t(R)emove stock from inventory"); System.out.println("\t(D)isplay stock level"); System.out.println("\t(C)heck if stock level is critical"); System.out.print("\t(Q)uit program"); System.out.println(); System.out.print("Selection: "); } protected void readMenuOption () { menuOption = (char) Console.in.readChar(); Console.in.readLine(); System.out.println(); } }// End of class CommandProcesor

James Tam The Inventory Class public class Inventory { public final static int CRITICAL = 10; public final static int MIN = 0; public final static int MAX = 100; private int stockLevel; private boolean amountInvalid;

James Tam The Inventory Class (2) public void getAmountToAdd () { int amount; do { System.out.print("No. items to add: "); amount = Console.in.readInt(); Console.in.readLine(); try { addToInventory(amount); amountInvalid = false; }

James Tam The Inventory Class (3) catch (InventoryOverMaxException e) { System.out.println(e); System.out.println("Enter another value."); System.out.println(); amountInvalid = true; } finally { displayInventoryLevel(); } } while (amountInvalid == true); }// End of method getAmountToAdd

James Tam The Inventory Class (4) public void getAmountToRemove () { int amount; do { System.out.print("No. items to remove: "); amount = Console.in.readInt(); Console.in.readLine(); try { removeFromInventory(amount); amountInvalid = false; }

James Tam The Inventory Class (5) catch (InventoryBelowMinException e) { System.out.println(e); System.out.println("Enter another value."); System.out.println(); amountInvalid = true; } finally { displayInventoryLevel(); } } while (amountInvalid == true); }// End of method getAmountToRemove

James Tam The Inventory Class (6) private void addToInventory (int amount) throws InventoryOverMaxException { int temp; temp = stockLevel + amount; if (temp > MAX) { throw new InventoryOverMaxException ("Adding " + amount + " item will cause stock to become greater than " + MAX + " units"); } else { stockLevel = stockLevel + amount; }

James Tam The Inventory Class (7) private void removeFromInventory (int amount) throws InventoryBelowMinException { int temp; temp = stockLevel - amount; if (temp < MIN) { throw new InventoryBelowMinException ("Removing " + amount + " item will cause stock to become less than " + MIN + " units"); } else { stockLevel = temp; }

James Tam The Inventory Class (8) public boolean inventoryTooLow () { if (stockLevel < CRITICAL) return true; else return false; } public void displayInventoryLevel () { System.out.println("No. items in stock: " + stockLevel); } }// End of class Inventory

James Tam Class InventoryOverMaxException public class InventoryOverMaxException extends Exception { public InventoryOverMaxException () { super (); } public InventoryOverMaxException (String s) { super (s); }

James Tam Class InventoryBelowMinException public class InventoryBelowMinException extends Exception { public InventoryBelowMinException () { super(); } public InventoryBelowMinException (String s) { super(s); }

James Tam Review: Inheritance Remember: You can substitute instances of a sub class for instances of a super class. This is because the sub class is an instance of the super class Monster Dragon BlueDragon A Dragon is a Monster A BlueDragon is a Dragon A Monster is not a Dragon A Dragon is not a BlueDragon

James Tam Inheritance Hierarchy For IOExceptions IOException EOFExceptionFileNotFound Exception These classes are instances of class IOException

James Tam 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 Inheritance And Catching Exceptions (2) try { } catch (IOException e) { } catch (EOFException e) { } try { } catch (EOFException e) { } catch (IOException e) { } Correct Incorrect

James Tam You Should Now Know The benefits of handling errors with an exception handler rather than employing a series of return values and conditional statements. How to handle exceptions Being able to call a method that may throw an exception by using a try-catch 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 What is the difference between a checked and an unchecked exception How to write your classes of exceptions The effect of the inheritance hierarchy when catching exceptions