Java Exception Handling - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Java Exception Handling

Description:

The Error class handles system-oriented, catastrophic failure at run-time. ... absolutely must be executed before a method returns is put in a finally block. ... – PowerPoint PPT presentation

Number of Views:150
Avg rating:3.0/5.0
Slides: 13
Provided by: drewh6
Category:

less

Transcript and Presenter's Notes

Title: Java Exception Handling


1
JavaException Handling
2
Exception
  • An exception is an exceptional condition that
    arises in a code sequence at run time.
  • A Java exception is an object that describes an
    exceptional condition that happens.
  • When an exception condition arises, an object
    representing that exception is created and thrown
    in the method that caused the error.

3
Exception Types
  • All exception types are subclasses of the
    built-in class Throwable.
  • There are two subclasses of the Throwable
    Exception and Error.
  • Exception is the class that you will create
    subclass for your own expception handling. The
    Error class handles system-oriented, catastrophic
    failure at run-time.

4
Run-time Default Exception Handler
  • When there is no code for exception handling and
    an error is detected, Java rum-time will
    construct a new exception object and then throws
    this exception.
  • The default exception handler displays a string
    describing the exception, prints a stack trace
    from the point at which the exception occurred,
    and terminates the program. (ExceptionNo1.java)

5
Exception Handling
  • Java exception can be handled manually by your
    own code via five keywords.
  • Program statements that you want to monitor for
    exceptions are contained within a try block.
  • If an exception occurs within the try block, it
    is thrown and caught by the catch keyword.

6
Exception Handling
  • To manually throw an exception, use the keyword
    throw.
  • Any code that absolutely must be executed before
    a method returns is put in a finally block.
  • Manual exception handling allows you to fix the
    error and prevents the program from automatically
    terminating.

7
Try, catch, finally
  • The general form
  • try
  • block of code for monitoring errors
  • catch (ExceptionType1 exceptionObj)
  • block of code for exception handling
  • catch (ExceptionType2 exceptionObj)
  • finally
  • block of code to executed before try block
    ends
  • (Exception1.java)

8
Try, catch, finally
  • A try and its catchs and finally statement form a
    unit.
  • The scope of the catch clause is restricted to
    those statements specified by the immediately
    preceding try statement.
  • A catch statement cannot catch an exception
    thrown by another try statement.

9
Try, catch, finally
  • The goal of most well-constructed catch clauses
    should be to resolve the exception condition and
    then continue on as if the error had never
    happened.
  • (Exception2.java)

10
Multiple catch Clauses and Nested try
Statements
11
Java Built-in Exceptions
  • Inside the standard package java.lang, Java
    defines several exception classes.
  • The most general of these exceptions are
    subclasses of the standard type RuntimeException.
  • Since java.lang is automatically imported,
    exceptions derived from RuntimeException are also
    automatically available.

12
Creating Your Own Exception Subclasses
  • You can define your own exception handling
    subclasses under Exception.
  • Because Exception is subclass of Throwable, it
    has all the methods defined by Throwable.
  • (Exception3.java)
Write a Comment
User Comments (0)
About PowerShow.com