1 / 36

Exception handling

Exception handling. Dealing with life’s little surprises. Program correctness. The term program correctness refers to a program’s working as advertised; that is, it produces correct results for valid inputs

roz
Download Presentation

Exception handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception handling Dealing with life’s little surprises

  2. Program correctness • The term program correctness refers to a program’s working as advertised; that is, it produces correct results for valid inputs • A related concept is program reliability; this is a measure of how well a program performs under a variety of conditions (including invalid inputs) • A program can be correct, but not reliable • A program can be reliable, but not correct • Our goal is to produce programs with both these qualities

  3. Ensuring reliability • We do our best to ensure program correctness through a rigorous testing and debugging process • To ensure reliability, we must anticipate conditions that could cause problems, and try to deal with them before the problems occur • In Java, we have exception handling, a powerful tool for ensuring program reliability

  4. Exceptions • An error condition that occurs during program runtime is called an exception • Exceptions are represented by exception objects, which are generated (thrown) in response to error conditions • Java includes a rich set of routines for dealing with such circumstances: this is known as exception handling

  5. Catching exceptions: try/catch block • Consists of try block followed by one or more catch blocks • try block: encloses code that might throw an exception • catch block(s) deal with any exception(s) thrown

  6. Syntax for try/catch block try { // code that may throw an exception } catch (ExceptionType parameterName) { // code that handles an exception thrown // in the try block }

  7. Catching Exceptions • Statements in the try block are executed in sequence. • If no error occurs then no exception is thrown, all statements in the try block are executed and the catch block(s) will be skipped

  8. Catching Exceptions • When one of the statements throws an exception, control is passed to the matching catch block and statements inside the catch block are executed • Provided the catch block does not halt the program or return from the method, program execution continues at the first statement following the catch block • If more than one exception is possible, multiple catch blocks can be written

  9. Example int value; // number to be supplied by user String input; // value read from keyboard Scanner kb = new Scanner (System.in); System.out.print (“Enter a number: ”); input = kb.nextLine(); try { value = Integer.parseInt(); } catch (InputMismatchException e) { System.out.println (input + “is not valid.\n” + “Please enter digits only.”); }

  10. Exception classes • The previous example included an instance of InputMismatchException, one of many Exception classes defined in the Java API • Examples of exceptions you may have encountered as runtime errors include: • IOException • NoSuchMethodException • ArrayIndexOutOfBoundsException • NullPointerException

  11. Exception classes • All exception classes have the following characteristics: • a constructor that takes a single String argument (often just the name of the exception, in quotes) • a getMessage() method that can recover the String with which the exception object was constructed • Generally speaking, the String briefly describes the type of error encountered

  12. The catch block’s parameter • The syntax of the catch block is very similar to that of a method definition: catch (ExceptionType parameterName) { // code to execute if exception was thrown } • The catch block’s parameter does 2 things: • specifies the type of exception being handled • provides a name for the thrown object, so that the object’s method(s) can be called

  13. Multiple catch blocks • A single block of code may throw multiple exceptions • For example, consider the code fragment below: String input = kb.nextLine(); int x = Integer.parseInt(input); // could throw // InputMismatchException array[x] = x; // could throw // ArrayIndexOutOfBoundsException • When multiple types of exception objects may be thrown, the try block should be followed by multiple catch blocks • In any one execution of a try block, only one exception can be thrown, since execution of the try block stops if such an error occurs

  14. Multiple catch blocks • When multiple catch blocks are used, the order of the blocks sometimes matters • When an exception is thrown, the catch blocks are examined in order, and the first that will handle the error is executed • The catch blocks that handle the least specific exception types should be placed last, so the that a more specific exception can be caught by the most appropriate catch block • Failure to place catch blocks in the correct order (specific to general) results in a compiler warning

  15. The Exception Class • The Java API’s default java.lang package contains the Exception class, from which more specific types of exceptions may be derived • All of the exception classes defined in the Java API inherit their common characteristics from the Exception class

  16. Throwing exceptions • We can define our own error conditions, and set up mechanisms to deal with them, using our own Exception objects • The code on the next slide, for instance, expands the previous numeric input example by imposing upper and lower limits on the int value that can be entered

  17. Example System.out.print (“Enter a number between 1 and 100: ”); input = kb.nextLine(); try { value = Integer.parseInt(); if (value < 1 || value > 100) throw new Exception (“value out of range”); } catch (InputMismatchException e) { System.out.println (input + “is not valid.\n” + “Please enter digits only.”); } catch (Exception e) { System.out.println (e.getMessage()); }

  18. An exception-controlled loop • The previous example handled the case of bad numeric input by printing an error message appropriate to the problem (not a number vs. an invalid number) • A more robust program would go one step further: get new input to replace the bad • An exception-controlled loop like the one shown on the next slide takes this extra step

  19. Example – assumes existence of instance variable kb and constants UPPER and LOWER public int getNumData (String prompt) { String inputStr; int num; while (true) { inputStr = kb.nextLine(); try { num = Integer.parseInt(inputStr); if (num < LOWER || num > UPPER) throw new Exception("Input out of bounds"); return num; // input okay so return the value & exit, ending loop } catch (InputMismatchException e) { System.out.println(inputStr + " is invalid\n" + "Please enter digits only"); } catch (Exception e) { System.out.println(e.getMessage()); } // end catch block } // end while loop } // end method

  20. Checked & unchecked exceptions • There are two general types of exceptions that may occur within programs: • checked exceptions are those that must be accounted for or the program won’t compile • unchecked exceptions are those that the compiler doesn’t account for; hence the name • Most of the exceptions we have discussed so far have been unchecked exceptions

  21. Not catching exceptions • As stated on the previous slide, there are some types of exceptions that we must handle in order for our programs to compile • There are times when we may not wish to catch exceptions in our code, but are forced to deal with them because they are checked • In such a situation, a throws clause can be used instead of a try/catch block

  22. Example • Suppose you have a method that appears to you to be coded correctly, but the compiler keeps coming back with an error message like the following: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown • This message results from an attempt to compile a method with a checked exception that has not been handled

  23. Example The class shown below contains a main method that would produce the compiler message shown on the previous slide: public class ThreadTest { public static void main(String [] args) { Thread.sleep(100); System.out.println ("2"); } } One way to take care of the exception and get rid of the error message is shown on the following slide

  24. Example public class ThreadTest { public static void main(String [] args) throws InterruptedException { System.out.println ("1"); Thread.sleep(100); System.out.println ("2"); } } The “throws clause,” which is part of the method heading, simply indicates that we acknowledge that an exception might occur, and that it will be handled elsewhere. This is the default behavior with unchecked exceptions; we only have to add a throws clause if our code could throw a checked exception.

  25. Defining exception classes • We have already seen that an exception object can be instantiated within a try block, and that a corresponding catch block can be written to deal with the object if thrown • In examples so far, all of the objects have been Exception objects – but they could be of any exception type • We can also define new exception classes

  26. Defining exception classes • You can define your own exception classes to cover cases not defined by the standard exception classes • You can take advantage of Java’s inheritance mechanisms to simplify the process

  27. Inheritance basics • A parent class (aka superclass) is a class with variables and methods that can be “passed on” to classes derived from it • A child class (aka subclass or derived class) automatically contains all inherited members, and can add more members or modify the inherited members as necessary

  28. Inheriting from Exception • As previously mentioned, the Exception class has a constructor that takes a String argument, and a getMessage method that returns the String • Child classes derived from Exception can be defined very simply, by defining a set of constructors that refer back to the inherited constructors – see next slide

  29. Example • public class MyNewException extends Exception { • public MyNewException () { • super (“I take exception to that!”); • } • public MyNewException (String msg) { • super (msg); • } • } • Notes: • the keyword extends indicates an inheritance relationship with the named superclass • the keyword super is used to call the superclass’s constructor

  30. Example • To test the new exception class, we’ll create two more classes: • The Question class method askQuestion contains a line of code that instructs the program to throw an exception under certain circumstances • The QuestionUser class’s main method calls askQuestion and contains the code to handle the exception if it’s thrown

  31. Question class import java.util.*; public class Question { public void askQuestion () throws MyNewException { Scanner kb = new Scanner(System.in); System.out.print ("Who is the fairest one of all? "); String answer = kb.nextLine(); if (!answer.equalsIgnoreCase("Cate")) throw new MyNewException(); else System.out.println("You are so right!"); } }

  32. QuestionUser class public class QuestionUser { public static void main (String [] args) { Question q = new Question(); try { q.askQuestion(); } catch (MyNewException e) { System.out.println(e.getMessage()); } } }

  33. Sample output Who is the fairest one of all? cate You are so right! Who is the fairest one of all? Snow White I take exception to that!

  34. OK, but seriously … • When possible, you should catch exceptions • Checked exceptions must be caught – the compiler will tell you about these. You can choose to handle them yourself, or you can put a throws clause in your method header • Unchecked exceptions (also known as runtime exceptions), such as invalid input, should be handled using try/catch blocks in your code – this will make your code more robust

  35. When to throw exceptions • The example we looked at earlier was a silly one, and could have been handled much more smoothly (and in a single class) with a simple if/else statement • In other words, this is an example of when NOT to throw an exception

  36. When to throw exceptions • To quote the text: “Reserve throw statements for situations in which the way the exceptional condition is handled depends on how and where the method is used. If the way the exceptional condition is handled depends on how and where the method is invoked, then the best thing to do is to let the programmer who invokes the method handle the exception. In all other situations, it is preferable to avoid throwing exceptions.” • Read the example on page 556 to see a serious example of exception throwing

More Related