Java Exception Handling
Java Exception Handling
Chittaranjan Pradhan
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Chittaranjan Pradhan Exceptions
Exceptions Exceptions
Exception Handling
Exception Sources
• Exception is an abnormal condition that arises when Java Built-In
executing a program. Exception is an error which can be Exceptions
Unchecked Built-In
handled. But, an error is an error which can’t be handled Exceptions
Checked Built-In Exceptions
Own Exception
• In contrast, Java: Handling
Try and Catch
• provides syntactic mechanisms to signal, detect and handle Exception Display
the absence of errors and the code to handle various kinds Creating Exceptions
throws Statement
of errors finally Block
11.2
Exception Handling
Exception Handling
Chittaranjan Pradhan
Exceptions
Exception Handling Exception Handling
Exception Sources
Java Built-In
• An exception is an object that describes an exceptional Exceptions
condition (error) that has occurred when executing a Unchecked Built-In
Exceptions
Exception Constructs
Exception-Handling Block
User-Defined
Exceptions
11.3
Exception Handling
Exception Sources
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exception Sources Exceptions
Unchecked Built-In
Exceptions can be: Exceptions
Checked Built-In Exceptions
Own Exception
Handling
• Such exceptions are typically used to report some error Multiple Catch Clauses
Nested try Statements
conditions to the caller of a method Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.4
Exception Handling
Java Built-In Exceptions
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
• unchecked exceptions - the compiler does not check if a Stack Trace Display
Own Exception
method handles or throws there exceptions Handling
Try and Catch
Exception Display
• checked exceptions - must be included in the method’s Multiple Catch Clauses
throws clause if the method generates but does not handle Nested try Statements
Throwing Exceptions
them Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.5
Exception Handling
Unchecked Built-In Exceptions
Chittaranjan Pradhan
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.6
Exception Handling
Checked Built-In Exceptions
Chittaranjan Pradhan
the java compiler. If some code within a method throws a Java Built-In
Exceptions
checked exception,then the method must either handle the Unchecked Built-In
Exceptions
exception or it must specify the exception using throws keyword Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.7
Exception Handling
Exception Constructs
Chittaranjan Pradhan
Exception Constructs
• catch: together with try, catches specific kinds of Exception-Handling Block
Exception Hierarchy
exceptions and handles them in some way Uncaught Exception
Default Exception Handler
Stack Trace Display
User-Defined
• throws: specifies which exceptions a given method can Exceptions
throw
11.8
Exception Handling
Exception-Handling Block
Chittaranjan Pradhan
• try {...} is the block of code to monitor for exceptions Own Exception
Handling
Try and Catch
Exception Display
• catch(Exception ex) {...} is exception handler for the Multiple Catch Clauses
Nested try Statements
11.9
Exception Handling
Exception Hierarchy
Chittaranjan Pradhan
Exceptions
Exception Hierarchy Exception Handling
Exception Sources
Java Built-In
• All exceptions are sub-classes of the build-in class Exceptions
Unchecked Built-In
Throwable Exceptions
Checked Built-In Exceptions
Exception Constructs
• Throwable contains two immediate sub-classes: Exception-Handling Block
Exception Hierarchy
• Exception: exceptional conditions that programs should Uncaught Exception
11.10
Exception Handling
Uncaught Exception
Chittaranjan Pradhan
Uncaught Exception
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
• When the Java run-time system detects the attempt to Exception Display
Multiple Catch Clauses
divide by zero, it constructs a new exception object and Nested try Statements
Throwing Exceptions
throws this object Creating Exceptions
throws Statement
finally Block
Exceptions
Default Exception Handler Exception Handling
Exception Sources
Exception Constructs
Exception-Handling Block
• This default handler: Exception Hierarchy
Uncaught Exception
• displays a string describing the exception Default Exception Handler
Stack Trace Display
• prints the stack trace from the point where the exception
Own Exception
occurred Handling
• terminates the program Try and Catch
Exception Display
java.lang.ArithmeticException: /by zero at Exc0.main Multiple Catch Clauses
Nested try Statements
(Exc0.java:4) Throwing Exceptions
Creating Exceptions
throws Statement
User-Defined
ultimately processed by the default handler Exceptions
11.12
Exception Handling
Stack Trace Display
Chittaranjan Pradhan
Stack Trace Display
Exceptions
• Stack trace is actually a record of the active stack frames Exception Handling
Java Built-In
debugging Exceptions
Unchecked Built-In
• The stack trace displayed by the default error handler Exceptions
Checked Built-In Exceptions
shows the sequence of method invocations that led up to Exception Constructs
the error Exception-Handling Block
Exception Hierarchy
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.13
Exception Handling
Own Exception Handling
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Own Exception
• if we detected the error, we can try to fix it Handling
• we prevent the program from automatically terminating Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
• Exception handling is done through the try and catch block Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.14
Exception Handling
Try and Catch
Chittaranjan Pradhan
Java Built-In
• catch specifies which exception we want to handle and Exceptions
Unchecked Built-In
how Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
• control moves immediately to the catch block: Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.15
Exception Handling
Try and Catch...
Chittaranjan Pradhan
Exceptions
Try and Catch... Exception Handling
Exception Sources
Java Built-In
• The scope of catch is restricted to the immediately Exceptions
preceding try statement -it cannot catch exceptions thrown Unchecked Built-In
Exceptions
Checked Built-In Exceptions
by another try statements
Exception Constructs
Exception-Handling Block
Exception Hierarchy
• Resumption occurs with the next statement after the Uncaught Exception
Default Exception Handler
try/catch block: Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
11.16
Exception Handling
Try and Catch...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.17
Exception Handling
Exception Display
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Display Exception Sources
Java Built-In
Exceptions
• All exception classes inherit from the Throwable class Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
• The following text will be displayed: Creating Exceptions
throws Statement
Exception: java.lang.ArithmeticException: / by zero finally Block
User-Defined
Exceptions
11.18
Exception Handling
Multiple Catch Clauses
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
• when an exception is thrown, the first one whose type Stack Trace Display
Own Exception
matches that of the exception is executed Handling
Try and Catch
Exception Display
Multiple Catch Clauses
• after one catch executes, the other are bypassed and the Nested try Statements
User-Defined
Exceptions
11.19
Exception Handling
Multiple Catch Clauses...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.20
Exception Handling
Multiple Catch Clauses...
Chittaranjan Pradhan
Multiple Catch Clauses...
Exceptions
• Order is important: Exception Handling
Exception Sources
• catch clauses are inspected top-down
Java Built-In
• a clause using a super-class will catch all sub-class Exceptions
exceptions Unchecked Built-In
Exceptions
Checked Built-In Exceptions
• Therefore, specific exceptions should appear before more
Exception Constructs
general ones. In particular, exception sub-classes must Exception-Handling Block
Exception Hierarchy
appear before super-classes Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.21
Exception Handling
Nested try Statements
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Nested try Statements Java Built-In
Exceptions
The try statements can be nested: Unchecked Built-In
Exceptions
Checked Built-In Exceptions
• If an inner try does not catch a particular exception the
Exception Constructs
exception is inspected by the outer try block Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
• This continues until: Stack Trace Display
• In the latter case, the Java run-time system will handle the Nested try Statements
Throwing Exceptions
User-Defined
Exceptions
11.22
Exception Handling
Nested try Statements...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.23
Exception Handling
Throwing Exceptions
Chittaranjan Pradhan
Own Exception
• the flow of control stops immediately Handling
• the nearest enclosing try statement is inspected if it has a Try and Catch
Exception Display
catch statement that matches the type of exception: Multiple Catch Clauses
clause, the default exception handler halts the program and User-Defined
Exceptions
prints the stack
11.24
Exception Handling
Creating Exceptions
Chittaranjan Pradhan
Exceptions
Exception Handling
Creating Exceptions Exception Sources
Java Built-In
Two ways to obtain a Throwable instance: Exceptions
Unchecked Built-In
• All Java built-in exceptions have at least two constructors: Exception Constructs
Exception-Handling Block
one without parameters and another with one String Exception Hierarchy
parameter: Uncaught Exception
Default Exception Handler
Stack Trace Display
User-Defined
Exceptions
11.25
Exception Handling
Creating Exceptions...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.26
Exception Handling
throws Statement
Chittaranjan Pradhan
throws Statement
If a method is capable of causing an exception that it does not Exceptions
Exception Handling
handle, it must specify this behavior by the throws clause in its Exception Sources
} Exception Constructs
Exception-Handling Block
where exception-list is a comma-separated list of all types of Exception Hierarchy
Uncaught Exception
exceptions that a method might throw Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.27
Exception Handling
throws Statement...
Chittaranjan Pradhan
throws Statement...
Exceptions
The throwOne method throws an exception that it does not Exception Handling
catch, nor declares it within the throws clause. Therefore the Exception Sources
Java Built-In
previous program does not compile Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.28
Exception Handling
finally Block
Chittaranjan Pradhan
finally Block
Exceptions
• When an exception is thrown: Exception Handling
Exception Sources
Exception Constructs
• For instance, if a method opens a file on entry and closes Exception-Handling Block
on exit; exception handling should not bypass the proper Exception Hierarchy
Uncaught Exception
closure of the file Default Exception Handler
Stack Trace Display
• The finally block will execute whether or not an exception Own Exception
Handling
is thrown Try and Catch
Exception Display
• The try/catch statement requires at least one catch or Multiple Catch Clauses
Java Built-In
• uncaught exception or Exceptions
• explicit return Unchecked Built-In
Exceptions
Checked Built-In Exceptions
• The finally clause is executed just before the method
Exception Constructs
returns Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.30
Exception Handling
finally Block...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.31
Exception Handling
finally Block...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.32
Exception Handling
User-Defined Exceptions
Chittaranjan Pradhan
User-Defined Exceptions
Exceptions
Exception Handling
• Build-in exception classes handle some generic errors. For Exception Sources
Java Built-In
application-specific errors define your own exception Exceptions
11.33
Exception Handling
User-Defined Exceptions...
Chittaranjan Pradhan
User-Defined Exceptions...
Exceptions
Exception Handling
Java Built-In
• String getMessage(): returns a description of the exception Exceptions
• StackTraceElement[] getStackTrace(): returns an array that Unchecked Built-In
Exceptions
contains the stack trace; the method at the top is the last Checked Built-In Exceptions
causeExc with the invoking exception as its cause, returns Uncaught Exception
Default Exception Handler
the exception reference Stack Trace Display
11.34
Exception Handling
User-Defined Exceptions...
Chittaranjan Pradhan
Exceptions
Exception Handling
Exception Sources
Java Built-In
Exceptions
Unchecked Built-In
Exceptions
Checked Built-In Exceptions
Exception Constructs
Exception-Handling Block
Exception Hierarchy
Uncaught Exception
Default Exception Handler
Stack Trace Display
Own Exception
Handling
Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
Creating Exceptions
throws Statement
finally Block
User-Defined
Exceptions
11.35