Exception handling in Java - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Exception handling in Java

Description:

An exception is an event, which occurs during the execution of a program, that ... A method that throws an uncaught, checked exception must include a throws clause ... – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 16
Provided by: patric190
Category:

less

Transcript and Presenter's Notes

Title: Exception handling in Java


1
Exception handling in Java
2
What is an Exception?
  • Definition
  • An exception is an event, which occurs during
    the execution of a program, that disrupts the
    normal flow of the program's instructions.
  • Some terms
  • Exception object containing information about
    the error, including its type and the state of
    the program when the error occurred
  • Throwing an exception Creating an exception
    object and handing it to the runtime system
  • Exception handler a block of code that can
    handle the exception.

3
Examples
  • IndexOutOfBoundsException
  • Accessing an invalid array element (length gtilt0)
  • FileNotFoundException
  • Trying to open a file that does not exist
  • NumberFormatException
  • Finding a non-decimal character when reading in a
    value
  • ArithmeticException
  • Attempting to divide by zero

4
Exception Handling
  • A program can deal with an exception in one of
    three ways
  • ignore it
  • If the exception is not handled the program will
    abort and print an error message
  • handle it where it occurs
  • Try-catch
  • elsewhere propagated (passed) back to the calling
    method(s) until it is dealt with by a try-catch
    statement
  • An exception can be handled at a higher level if
    it is not appropriate to handle it where it
    occurs.
  • The Exception propagates up through the method
    calling hierarchy until they are caught and
    handled or until they reach the level of the main
    method in which case it is either handled there
    (if there is a handler) or the program terminates

5
Exception Propagation
                                                
    .

Searching the call stack for the exception
handler
Call stack
6
Exception Handler in Java
  • To write an exception handler in Java, you need
    to understand how to use the following three
    components
  • The try block
  • The catch block
  • The finally block

7
Try-catch-finally
  • try
  • statements
  • catch (exceptionType1 identifier1)
  • // one or multiple
  • catch (exceptionType2 identifier2)
  • statements
  • ...
  • finally
  • // one or none
  • statements

8
The try Block
  • The first step in constructing an exception
    handler is to enclose the code that might throw
    an exception within a try block.
  • A try block is followed by one or more catch
    clauses, which contain code to process an
    exception

try BufferedReader in new
BufferedReader(new FileReader(selectedFileOpe
n)) String line in.readLine() while(line
! null) line in.readLine() catch(I
OException e)
9
The catch block
  • If an exception occurs within the try block, that
    exception is handled by an exception handler
    associated with it.
  • To associate exception handlers with a try block,
    you must provide one or more catch blocks
    directly after the try block. No code can be
    between the end of the try block and the
    beginning of the first catch block.
  • Each catch clause has an associated exception
    type
  • When an exception occurs, processing continues at
    the first catch clause that matches the exception
    type
  • only first matching catch block will be executed
    even if more specific exception later in
    statement

10
The finally Clause
  • A try statement can have an optional clause
    designated by the reserved word finally
  • If no exception is generated, the statements in
    the finally clause are executed after the
    statements in the try block complete
  • Also, if an exception is generated, the
    statements in the finally clause are executed
    after the statements in the appropriate catch
    clause complete

11
Specifying the Exceptions Thrown by a Method
  • In some cases, it is better to let a method
    further up the call stack to catch exceptions
    that occur within it
  • Adding a throws clause to the method declaration
  • The throws clause comprises the throws keyword
    followed by a comma-separated list of all the
    exceptions thrown by that method
  • The clause goes after the method name and
    argument list and before the brace that defines
    the scope of the method
  • public void writeToFile() throws IOException

12
How to throw an exception?
  • Before you can catch an exception, some code
    somewhere must throw one
  • All methods use the throw statement to throw an
    exception
  • It requires a single argument a throwable
    object, which is an instance of any subclass of
    the Throwable class
  • you can throw only objects that inherit from the
    java.lang.Throwable class
  • A method that throws an uncaught, checked
    exception must include a throws clause in its
    declaration
  • The exception is not a checked one, your method
    do not need to contain a throws clause.

13
Exception Hierarchy
14
How to create your own exception classes?
  • To create your own exception classes, you need
    only declare it as a subclass of some member of
    the Exception family.

public class EmptyStackException extends
RuntimeException public EmptyStackException()
super ("The stack is empty.")
public EmptyStackException (String
message) super (message)
15
Exercises
  • (1) Is there anything wrong with this exception
    handler as written? Will this code compile?
  • (2) What is wrong with using the first exception
    handler?

try catch (Exception e) catch
(ArithmeticException a)
Write a Comment
User Comments (0)
About PowerShow.com