Sie sind auf Seite 1von 2

Handling Exceptions

6COMPRG1

An EXCEPTION is an occurrence of an undesirable situation that can be detected


during program execution.

Examples:
-division by zero
-inputting invalid data
-array index out of bounds

Handling Exceptions within a Program


-Conditional Statements may be used(if/else)
-Java’s Mechanism of Exception Handling
*when an exception occurs, an object of a particular exception class is
created. Java provides several exception classes to handle certain exceptions. The
class Exception is the superclass of all the exception classes in Java.

try/catch/finally block
try
{ //statements }
catch(ExceptionClassName1 objRef1)
{ //exception handler code } …
catch(ExceptionClassNameN objRefN)
{ //exception handler code }
finally
{ //statements }

o Statements that might generate an exception are placed in the try block. It
might also contain statements that should not be executed if an exception
occurs.
o A catch block specifies the type of exception it can catch and contains an
exception handler.
o Any code in the finally block always executes, regardless of whether an
exception occurs, except when the program exits early by calling the
System.exit method.
o If no exception is thrown in a try block, all catch blocks associated with the try
block are ignored and program execution resumes after the last catch block.
o If an exception is thrown, in a try block, the remaining statements in the try
block are ignored. The program searches the catch blocks in the order in
which they appear after the try block and looks for an appropriate exception
handler.
o If the type of the thrown exception matches the parameter type in one of the
catch blocks, the code of that catch block executes and the remaining catch
blocks after this catch block are ignored.
o If there is a finally block after the last catch block,the finally block executes
regardless of whether an exception occurs.

catch block
catch (ArithmeticException aeRef)
{ //exception handler code }
This catch block catches an exception of type ArithmeticException. The identifier aeRef
is a reference variable of type ArithmeticException. aeRef contains the address of
the exception object, the exception object can be accessed through the variable
aeRef. It stores the detailed description of the thrown exception.
*the method toString may be used to retrieve the message containing the description of
the thrown exception.

Order of catch blocks


Suppose that an exception occurs in a try block and that exception is caught by a catch
block. Then, the remaining catch blocks associated with the try block are ignored.
Therefore, the order in which catch blocks are listed should be to put more specific
exceptions before less specific exceptions.

Common Exception Classes


ArithmeticException – arithmetic errors such as division by zero.
ArrayIndexOutOfBoundsException – Array index is either less than 0 or greater than or
equal to the length of the array.
IllegalArgumentException – calling a method with illegal arguments.
IndexOutOfBoundsException – an array or a string index is out of bounds.
Common Exception Classes
NullPointerException – reference to an object that has not been instantiated.
InputMismatchException – input retrieved does not match the pattern for the expected
type, or the input is out of range for the expected type.
NumberFormatException – use of an illegal number format.

Das könnte Ihnen auch gefallen