Handling Exceptions - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Handling Exceptions

Description:

trying to open an input file that does not exist. an array index that goes ... If an exception in a try block is caught by the first catch block, the remaining ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 20
Provided by: manasibhat
Category:

less

Transcript and Presenter's Notes

Title: Handling Exceptions


1
Handling Exceptions
2
Exception
  • Definition an occurrence of an undesirable
    situation that can be detected during program
    execution
  • Examples
  • division by zero
  • trying to open an input file that does not exist
  • an array index that goes out of bounds

3
Java Exception Hierarchy
4
Constructors and Methods of the class Throwable
5
The class Exception and its Subclasses from
java.lang
6
The class Exception and its Subclasses from
java.io
7
Javas Exception Class
  • class Exception
  • Subclass of class Throwable
  • Superclass of classes designed to handle
    exceptions
  • Various types of exceptions
  • I/O exceptions
  • Number format exceptions
  • File not found exceptions
  • Array index out of bounds exceptions
  • Various exceptions categorized into separate
    classes and contained in various packages

8
The class Exception and its Constructors
9
Java Exception Classes
10
Exceptions Thrown by Methods
11
Checked Exceptions
  • Definition any exception that can be analyzed by
    the compiler
  • Examples
  • IOExceptions

12
Unchecked Exceptions
  • Definition exceptions that cannot be analyzed
    when the program compiles (must be checked for by
    programmer)
  • Examples
  • Division by zero
  • Array index out of bounds
  • Syntax
  • throws ExceptionType1, ExceptionType2,
  • ExceptionType1, ExceptionType2, etc are names of
    exception classes

13
Exceptions Example Code
  • public static void exceptionMethod()
  • throws
    NumberFormatException, IOException
  • //statements
  • The method exceptionMethod throws exceptions of
    the type NumberFormatException and IOException.

14
Handling Exceptions within a Program
  • try/catch/finally block used to handle exceptions
    within a program
  • try block
  • Includes statements that may generate an
    exception
  • Includes statements that should not be executed
    if an exception occurs
  • Followed by zero or more catch blocks
  • May or may not be followed by finally block

15
Handling Exceptions within a Program
  • catch block
  • Heading specifies type of exception it can catch
  • Contains exception handler completely handles
    exception
  • Can catch all exceptions of a specific type or
    all types of exceptions
  • May or may not be followed by a finally block
  • finally block
  • Code contained in this block always executes
  • try block with no catch block has finally block

16
Handling Exceptions within a Program
  • try
  • //statements
  • catch(ExceptionClassName1 objRef1)
  • //exception handler code
  • catch(ExceptionClassName2 objRef2)
  • //exception handler code
  • ...
  • catch(ExceptionClassNameN objRefN)
  • //exception handler code
  • finally

17
Order of catch Blocks
  • If an exception in a try block is caught by the
    first catch block, the remaining catch blocks are
    ignored
  • Must be careful about order catch blocks are
    listed

18
Combining catch Blocks With the Operator
instanceof
19
The Method printStackTrace
  • Used to determine the order in which the methods
    were called and where the exception was handled
Write a Comment
User Comments (0)
About PowerShow.com