1 / 46

Exception Handling

Cs442: Advanced Java Programming . Exception Handling. Chapter 11. Exception. is an indication of a problem that occurs during a program’s execution. Divide by zero . First program:. output of first program. : Successful division. unsuccessful division : problem is divide by zero.

gada
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. Cs442: Advanced Java Programming Exception Handling Chapter 11

  2. Exception is an indication of a problem that occurs during a program’s execution.

  3. Divide by zero First program:

  4. output of first program :Successful division

  5. unsuccessful division : problem is divide by zero Stack trace Exception detected in line 27 in main . Exception occur in line 18 in quotient . Throw point: is the initial point at which the exception is occur In this example throw point is line 18 Type of Exception : java.lang.ArithmeticException this exception can arise from number of different problems in arithmetic. Type of problem : divide by zero

  6. Stack trace: a stack trace is a list of the method calls that the application was in the middle of when an Exception was thrown. Stack Syntax of stack trace statement : Name of package Name of class Name of method Name of file Line num

  7. output of first program Problem : input not match for the expected type Exception occur in line 26 in main. throw point is line 26 Type of Exception : java.util.InputMismatchExceptionthrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

  8. Exception handling Enables you to create applications that can resolve (or handle) exceptions. In many cases, handling an exception allows a program to continue executing as if no problem had been encountered.

  9. When to Use Exception Exception handling is designed to process synchronous errors , which occur when a statement executes. Example: • Out-of-range array indices • Arithmetic overflow • Division by zero

  10. Handling ArithmeticExceptions , InputMismatchException:

  11. :Successful division unsuccessful division : problem is divide by zero

  12. Problem : input not match for the expected type

  13. Try Block Try block : which encloses the code that might throw an exception and the code that should not execute if an exception occurs. if an exception occurs, the remaining code in the try block will be skipped.

  14. Catch Block Catch Block: catches and handles an exception. At least one catch block must immediately follow the try block. Each catch block specifies in parentheses an exception parameter that identifies the exception type the handler can process. When an exception occurs in a try block, the catch block that executes is the first one whose type matches the type of the exception that occurred Type of exception

  15. Common Programming Error It’s a syntax error to place code between a try block and its corresponding catch blocks. Each catch block can have only a single parameter , specifying a comma-separated list of exception parameters is a syntax error.

  16. Uncaught Exception Uncaught Exception: Is one for which there are no matching catch blocks.

  17. Exception Thrower Exception Thrower: is a method that throws an exception. Type of exception thrower: • catcher. • propagator.

  18. Exception catcher : is an exception thrower that includes a matching catch block for the thrown exception. • Exception propagator: does not contain a matching catch block. A method may be a catcher of one exception and a propagator of another.

  19. Exception catcher: Output:

  20. Exception Propagator If a method is an exception propagator, we need to modify its header to declare the type of exceptions the method propagates. We use the reserved word throws for this declaration. void C( ) throws Exception { } void D( ) throws Exception { }

  21. Exception Propagator Output:

  22. Without the required throws Exception clause is compilation error .

  23. However, for the exception of the type called runtime exceptions, the throws clause is optional.

  24. Java Exception Hierarchy All java exception classes inherit directly of indirectly from class Exception.

  25. Checked vs. Unchecked Exceptions

  26. Catching Subclass Exceptions If a catch handler is written to catch superclass-type exception objects, it can also catch all objects of that class’s subclasses. This enables catch to handle related errors with a concise notation and allows for polymorphic processing of related exceptions. You can certainly catch each subclass type individually if those exceptions require different processing.

  27. Example :

  28. Output unsuccessful division : problem is divide by zero Problem : input not match for the expected type

  29. If there are multiple catch blocks that match a particular exception type, only the first matching catch block executes when an exception of that type occurs.

  30. Common Programming Error Placing a catch block for a superclass exception type before other catch blocks that catch subclass exception types would prevent those catch blocks from executing, so a compilation error occurs.

  31. finally Block The finally block will execute whether or not an exception is thrown in the corresponding try block. The finally block also will execute if a try block exits by using a return, break or continue statement or simply by reaching its closing right brace. The finally block will not execute if the application exits early from a try block by calling method System.exit.

  32. Output:

  33. Stack Unwinding and Obtaining Information from an Exception Object When an exception is thrown but not caught in a particular scope, the method-call stack is “unwound,” and an attempt is made to catch the exception in the next outer try block. This process is called stack unwinding. Unwinding the method-call stack means that the method in which the exception was not caught terminates, all local variables in that method go out of scope and control returns to the statement that originally invoked that method. If a try block encloses that statement, an attempt is made to catch the exception. If a try block does not enclose that statement or if the exception is not caught, stack unwinding occurs again.

  34. Output:

  35. Obtaining Data from an Exception Object Class Throwable also provides a getStackTrace method that retrieves the stack-trace information that might be printed by printStackTrace. Class Throwable’sgetMessage method returns the descriptive string stored in an exception.

  36. Chained Exceptions Sometimes a method responds to an exception by throwing a different exception type that’s specific to the current application. If a catch block throws a new exception, the original exception’s information and stack trace are lost

  37. Example

  38. Output:

  39. Preconditions and Postconditions A precondition must be true when a method is invoked. Preconditions describe constraints on method parameters and any other expectations the method has about the current state of a program just before it begins executing. If the preconditions are not met, then the method’s behavior is undefined—it may throw an exception, proceed with an illegal value or attempt to recover from the error. You should not expect consistent behavior if the preconditions are not satisfied.

  40. A postconditionis true after the method successfully returns. Postconditions describe constraints on the return value and any other side effects the method may have. When defining a method, you should document all postconditions so that others know what to expect when they call your method, and you should make certain that your method honors all its postconditions if its preconditions are indeed met.

  41. Assertions Preconditions are assertions about its state when a method is invoked. postconditions are assertions about a program’s state after a method finishes.

  42. Output:

More Related