Exception - PowerPoint PPT Presentation

About This Presentation
Title:

Exception

Description:

... exception handler, no matter what method was executing when the exception was thrown. ... not handled by a more tightly nested block or method, statement1 ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 10
Provided by: University482
Learn more at: http://pirate.shu.edu
Category:
Tags: exception

less

Transcript and Presenter's Notes

Title: Exception


1
Exception
  • Error handling

2
Exception 4
  • An unusual occurrence during program execution
    that requires immediate handling
  • Errors are the most common type of exception
  • Java enables centralization of exception handling
    code in a location that is most logical
  • When an exception is reported (or thrown)
    execution is automatically transferred the
    closest exception handler, no matter what method
    was executing when the exception was thrown.

3
Try/catch 4
  • Try establishes a block of code that is to have
    its exceptions and abnormal exits (through break,
    continue,return, or exception propagation)
    handled
  • Catcha try block may be followed by zero or more
    catch clauses that specify code to handle various
    types of exceptions
  • Catch clauses have unusual syntax each is
    declared an argument, much like a method
    argument. This argument must be of type throwable
    or a subclass
  • Do not need a catch clause for every possible
    exception

4
Trycatch 4
  • try
  • statement1
  • catch(ExceptionClass1 e1)
  • statement2
  • catch(ExceptionClass2 e2)
  • statement3
  • finally
  • statementn
  • The statement following try is executed
    unconditionally as part of the normal flow of
    execution

5
Trycatch 3
  • During execution of this statement (or block), an
    exception may be raised (even indirectly, i.e.,
    statement may call a method that may raise an
    exception)
  • If the exception is raised and not handled by a
    more tightly nested block or method, statement1
    terminates execution and the interpreter checks
    if one of the catch blbocks can handle the
    exception
  • Every exception class implements the throwable
    interface

6
Exception 3
  • Statements inside the catch block are executed
    when the respective exception type is matched
  • Inside the catch block, the code may decline to
    handle the exception by executing a throw
    statement. Here, the exception is passed along
    and the interpreter must search for another
    handler
  • Exceptions not handled by any code eventually
    reach the Java default exception handler, which
    prints a message and terminates execution

7
Finally 3
  • Code in the finally block is executed
    unconditionally after execution leaves the try
    block
  • The finally block is guaranteed to be executed,
    whether the try block terminated normally or
    exited early because of a n exception or break
    statement
  • The finally block is a logical place to put code
    that cleans up

8
Exception No Exception
  • try
  • ?
  • exception raised?
  • ?
  • ? catch()
  • ?
  • ?
  • try
  • ?
  • ?
  • ? catch()
  • ?
  • ?

9
Console.java
  • public static int readInt()
  • String tmpX readString().trim()
  • try
  • return Integer.parseInt(tmpX)
  • catch(NumberFormatException ne)
  • System.err.println("Not an integer.
    Quitting ...")
  • System.exit(-1)
  • return -1
Write a Comment
User Comments (0)
About PowerShow.com