0% found this document useful (0 votes)
3 views

Java Exception Handling

Uploaded by

2105915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Exception Handling

Uploaded by

2105915
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Exception Handling

Chittaranjan Pradhan

Web Technology 11 Exceptions


Exception Handling

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
Chittaranjan Pradhan Exceptions

School of Computer Engineering,


KIIT University
11.1
Exception Handling
Exceptions
Chittaranjan Pradhan

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

• In the languages that do not support exception handling, Exception Constructs


Exception-Handling Block
errors must be checked and handled manually, usually Exception Hierarchy

through the use of error codes Uncaught Exception


Default Exception Handler
Stack Trace Display

Own Exception
• In contrast, Java: Handling
Try and Catch
• provides syntactic mechanisms to signal, detect and handle Exception Display

errors Multiple Catch Clauses


Nested try Statements
• ensures a clean separation between the code executed in Throwing Exceptions

the absence of errors and the code to handle various kinds Creating Exceptions
throws Statement
of errors finally Block

• brings run-time error management into object-oriented User-Defined


Exceptions
programming

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

program Checked Built-In Exceptions

Exception Constructs
Exception-Handling Block

• Exception handling involves the following: Exception Hierarchy


Uncaught Exception
Default Exception Handler
• when an error occurs, an object (exception) representing Stack Trace Display
this error is created and thrown in the method that caused it Own Exception
Handling
Try and Catch
• that method may choose to handle the exception itself or Exception Display

pass it on Multiple Catch Clauses


Nested try Statements
Throwing Exceptions
Creating Exceptions
• either way, at some point, the exception is caught and throws Statement

processed finally 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

• generated by the Java run-time system Exception Constructs


Exception-Handling Block
• Fundamental errors that violate the rules of the Java Exception Hierarchy
Uncaught Exception
language or the constraints of the Java execution Default Exception Handler

environment Stack Trace Display

Own Exception
Handling

• manually generated by programmer’s code Try and Catch


Exception Display

• 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

Java Built-In Exceptions Java Built-In


Exceptions
Unchecked Built-In

• The default java.lang package provides several exception Exceptions


Checked Built-In Exceptions

classes, all sub-classing the RuntimeException class Exception Constructs


Exception-Handling Block
Exception Hierarchy

• Two sets of build-in exception classes: Uncaught Exception


Default Exception Handler

• 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

Unchecked Built-In Exceptions


Exceptions
These are the exceptions that are not checked at compile time Exception Handling

and checked by the JVM 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.6
Exception Handling
Checked Built-In Exceptions
Chittaranjan Pradhan

Checked Built-In Exceptions Exceptions


Exception Handling
These are the exceptions that are checked at compile time by Exception Sources

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 Exceptions


Exception Handling
Five constructs are used in exception handling: Exception Sources

• try: a block surrounding program statements to monitor for Java Built-In


Exceptions
exceptions Unchecked Built-In
Exceptions
Checked Built-In Exceptions

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

• finally: specifies any code that absolutely must be Own Exception


Handling
executed whether or not an exception occurs Try and Catch
Exception Display
Multiple Catch Clauses
Nested try Statements
• throw: used to throw a specific exception from the Throwing Exceptions
Creating Exceptions
program throws Statement
finally Block

User-Defined
• throws: specifies which exceptions a given method can Exceptions

throw

11.8
Exception Handling
Exception-Handling Block
Chittaranjan Pradhan

Exception-Handling Block Exceptions


Exception Handling
Exception Sources
try {...}
Java Built-In
catch(Exception1 ex1) {...} Exceptions
Unchecked Built-In
catch(Exception2 ex2) {...} Exceptions
Checked Built-In Exceptions
...
Exception Constructs
finally {...} Exception-Handling Block
Exception Hierarchy
Uncaught Exception

where: Default Exception Handler


Stack Trace Display

• 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

exception Exception Throwing Exceptions


Creating Exceptions
throws Statement
finally Block
• finally {...} is the block of code to execute before the try User-Defined
block ends Exceptions

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

catch. The class includes: Default Exception Handler


Stack Trace Display
• RuntimeException: defined automatically for user programs to Own Exception
include: division by zero, invalid array indexing, etc Handling
Try and Catch
Exception Display
• User-defined exception classes Multiple Catch Clauses
Nested try Statements
Throwing Exceptions
• Error: exceptions used by Java to indicate errors with the Creating Exceptions
throws Statement
run-time environment; user programs are not supposed to finally Block

catch them User-Defined


Exceptions

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

• This will cause the execution of Exc0 to stop - once an User-Defined


Exceptions
exception has been thrown it must be caught by an
exception handler and dealt with
11.11
Exception Handling
Default Exception Handler
Chittaranjan Pradhan

Exceptions
Default Exception Handler Exception Handling
Exception Sources

• As we have not provided any exception handler, the Java Built-In


Exceptions
exception is caught by the default handler provided by the Unchecked Built-In
Exceptions
Java run-time system Checked Built-In Exceptions

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

• Any exception not caught by the user program is finally Block

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

generated by the execution of a program. It is used for Exception Sources

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

• Here, the exception is raised in subroutine() which is Uncaught Exception


Default Exception Handler

called by main(): 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.13
Exception Handling
Own Exception Handling
Chittaranjan Pradhan

Exceptions
Exception Handling
Exception Sources

Own Exception Handling Java Built-In


Exceptions
Unchecked Built-In
Exceptions
• Default exception handling is basically useful for Checked Built-In Exceptions

debugging Exception Constructs


Exception-Handling Block
Exception Hierarchy
Uncaught Exception
• Normally, we want to handle exceptions ourselves Default Exception Handler

because: Stack Trace Display

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

Try and Catch


Exceptions
Exception Handling
• try surrounds any code we want to monitor for exceptions Exception Sources

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

• The exception is handled and the execution resumes

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

• The purpose of catch should be to resolve the exception throws Statement


finally Block

and then continue as if the error had never happened User-Defined


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

• Throwable overrides toString() to describe the exception Exception Constructs


Exception-Handling Block
textually: 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
• 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

Multiple Catch Clauses Java Built-In


Exceptions
Unchecked Built-In
When more than one exception can be raised by a single piece Exceptions
Checked Built-In Exceptions
of code, several catch clauses can be used with one try block:
Exception Constructs

• each catch catches a different kind of exception Exception-Handling Block


Exception Hierarchy
Uncaught Exception
Default Exception Handler

• 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

execution continues after the try/catch block Throwing Exceptions


Creating Exceptions
throws Statement
finally Block

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

• one of the catch statements succeeds or Own Exception


Handling
• all the nested try statements are exhausted Try and Catch
Exception Display
Multiple Catch Clauses

• In the latter case, the Java run-time system will handle the Nested try Statements
Throwing Exceptions

exception Creating Exceptions


throws Statement
finally Block

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

Throwing Exceptions Exceptions


Exception Handling
Exception Sources
• The throw statement can be used either to throw our own Java Built-In
Exceptions
exceptions or to throw an instance of Java’s exception Unchecked Built-In

throw ThrowableInstance; Exceptions


Checked Built-In Exceptions

• ThrowableInstance must be an object of type Throwable or Exception Constructs


Exception-Handling Block
its subclass Exception Hierarchy
Uncaught Exception
• Once an exception is thrown by: Default Exception Handler

throw ThrowableInstance; Stack Trace Display

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

• if one exists, control is transferred to that statement Nested try Statements


Throwing Exceptions
• otherwise, the next enclosing try statement is examined Creating Exceptions
throws Statement
• if no enclosing try statement has a corresponding catch finally Block

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

• The creating one with the new operator Exceptions


Checked Built-In Exceptions

• 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

throw new NullPointerException("demo"); Own Exception


Handling
Try and Catch
Exception Display
• using a parameter of the catch clause: Multiple Catch Clauses
Nested try Statements
Throwing Exceptions

try {...} Creating Exceptions


throws Statement
catch(Throwable e) {... e ...} finally Block

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

declaration: Java Built-In


Exceptions
type name(parameter-list) throws exception-list { Unchecked Built-In
Exceptions

... Checked Built-In Exceptions

} 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

• the execution of a method is changed Java Built-In


Exceptions
• the method may even return prematurely
Unchecked Built-In
Exceptions
• This may be a problem is many situations Checked Built-In Exceptions

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

finally clause, although both are optional: Nested try Statements


Throwing Exceptions

try { ... } Creating Exceptions


throws Statement
catch(Exception1 ex1) { ... } ... finally Block

finally { ... } User-Defined


Exceptions
• Executed after try/catch whether of not the exception is
thrown
11.29
Exception Handling
finally Block...
Chittaranjan Pradhan
finally Block...
Exceptions
• Any time a method is to return to a caller from inside the Exception Handling

try/catch block via: Exception Sources

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

classes Unchecked Built-In


Exceptions
Checked Built-In Exceptions
• Define a subclass of Exception:
Exception Constructs
class MyException extends Exception { ... } Exception-Handling Block
Exception Hierarchy
Exception itself is a sub-class of Throwable Uncaught Exception
Default Exception Handler
• All user exceptions have the methods defined by the Stack Trace Display

Throwable class Own Exception


Handling
• Throwable fillInStackTrace(): returns a Throwable object that Try and Catch
Exception Display
contains a completed stack trace; the object can be Multiple Catch Clauses
rethrown Nested try Statements
Throwing Exceptions
• Throwable getCause(): returns the exception that underlies Creating Exceptions

the current exception. If no underlying exception exists, null throws Statement


finally Block
is returned User-Defined
• String getLocalizedMessage(): returns a localized Exceptions

description of the exception

11.33
Exception Handling
User-Defined Exceptions...
Chittaranjan Pradhan

User-Defined Exceptions...
Exceptions
Exception Handling

• ... Exception Sources

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

one called before exception Exception Constructs


Exception-Handling Block
• Throwable initCause(Throwable causeExc): associates Exception Hierarchy

causeExc with the invoking exception as its cause, returns Uncaught Exception
Default Exception Handler
the exception reference Stack Trace Display

• void printStackTrace(): displays the stack trace Own Exception


Handling
• void printStackTrace(PringStream stream): sends the stack Try and Catch

trace to the specified stream Exception Display


Multiple Catch Clauses
• void setStackTrace(StackTraceElement elements[]): sets the Nested try Statements
Throwing Exceptions
stack trace to the elements passed in elements; for Creating Exceptions

specialized applications only throws Statement


finally Block
• String toString(): returns a String object containing a
User-Defined
description of the exception; called by print() when Exceptions

displaying a Throwable object

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

You might also like