Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 code I.e. If your code knows what to do, it should do it. If not, throw an exception.

2 Example Integer.parseInt method takes a String and tries to interpret it as an int. –If it can, it returns an int value. –If it can’t, it throws an exception. Why is the exception thrown? For two reasons: –there is no int value it can return (through normal control flow) to signal a problem with the input –Integer.parseInt has no idea what the caller wants to do. The method does not know the context of the request.

3 Example (continued) Possible calling contexts A user-input routine that calls Integer.parseInt with a String that a user entered can, if an exception is thrown, prompt the user for new input. A non-UI routine that calls Integer.parseInt may be reading input from a file, and may take the exception as an indication that there is an error in the input file format, or perhaps that input processing must transition to another state.

4 Throwing exceptions syntax –throw new Exception() –throw new Exception(“A message”) exception classes –Exception is a subclass of Throwable –RuntimeException is a subclass of Exception any exception which is of this type is unchecked any exception which is NOT of this type is checked

5 unchecked exceptions “unchecked exceptions are generally the programmer’s fault” [H, pg. 19] the compiler doesn’t force you to deal with them in your program – they just shouldn’t occur in the first place e.g. NullPointerException

6 checked exceptions “In general, a checked exception is caused by an external condition beyond the programmer’s control.” [H, pg. 19] Your code must either –provide a handler for every checked exception, or –indicate that the exception is propagated further by declaring the exception as thrown by the method in its header

7 the ‘throws’ clause Examples from [h, pg. 20] public void read(String filename) throws FileNotFoundException { FileReader reader = new FileReader(filename); … } public void read(String filename) throws IOException, ClassNotFoundException { … }

8 catching exceptions Wrap the code from which you are prepared to handle exceptions in a throw block Provide one or more catch blocks, one for each type of exception your code will handle –catch blocks are attempted in the order given, and the first matching one is used, so list catch block from most specific first to most general last

9 finally block you may provide a finally block a finally block is always executed –if an exception is not thrown after all the code in the try block has finished normally –if an exception is thrown and is handled by a catch block after the exception handler finishes –if an exception is thrown but is not handled, before the uncaught exception is propagated further.

10 Example try { … } catch (ExceptionType1 e) { … } catch (ExceptionType2 e) { … } … catch (ExceptionTypeN e) { … } finally { … }

11 Defining your own exception class Subclass an appropriate existing exception class Provide no-argument and single-argument (String) constructors

12 The Error class The class Throwable has two subclasses: –Exception –Error What’s the difference –A program can typically recover from an exception and continue processing –A program can typically not recover from an error


Download ppt "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."

Similar presentations


Ads by Google