29
What is an exception? • An exception is: – an event that interrupts the normal processing of the program. – an error condition that violates the semantic rules of a Java program.

What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic

Embed Size (px)

Citation preview

What is an exception?

• An exception is:– an event that interrupts the normal

processing of the program.– an error condition that violates the

semantic rules of a Java program.

Exception Handling

• Essentially exception handling is Java’s method of dealing with programming errors. – Java will attempt to handle an error rather

than just abort the program.– Exception handling is intended for error

processing that is an infrequent activity.

Exception Handling

• Exception handling is intended to deal with synchronous errors like divide by zero, and array index out of bounds.

• Exception handling is NOT intended to deal with asynchronous errors such as mouse clicks, network messaging, and keyboard strokes.

Exception Handling

• Using exception handling is the preferred way to handle error conditions in your program.

• Exception handling allows you to divide your program into separate sections for the normal case and for the exception case.

Exception Handling

• Exception handling should be used only in situations where your program is able to recover from the problem and continue running.

Exception Handling

• Java throws an exception when it cannot complete it’s normal processing.– Array out of bounds, or divide by zero.

Exception Handling

• Examples– Divide By Zero– Array Out of Bounds

Exception Handling

• When Java throws an exception, it expects the exception to be caught. – When an exception is caught it means that

the error is handled and the program can continue to execute.

Exception Handling

• If an exception is thrown but your program does not catch the exception, then your program will abort.

• If an exception is thrown and your program catches the exception, then your program will continue to execute.

Exception Handling

• There is an Java object class called Exception. – This class or some subclass of Exception

are used to catch exceptions in a program.

Types of Exceptions

• A runtime exception occurs when your program is running. They occur in the Java runtime system.– An example is a divide by zero exception

and array index out of bounds.– This type of exception can occur anywhere

in your program.

Types of Exceptions

• A checked exception is verified by the Java compiler to be caught or ignored by any method that the exception can occur in.– A method ignores an exception by

throwing the exception in the method declaration.

public void someMethod () throws someException {...}

Types of Exceptions

• If a checked exception is not thrown then the method must catch the exception and deal with it.

• Checked exceptions are found at compile time and runtime exceptions are found while your program is running.

The Exception Class

• An exception is an object of the Exception class or one of its subclasses.– The constructor methods for the Exception

class are:• Exception() and Exception(String s)

• The second constructor provides an error string as a parameter to the exception object.

The Exception Class

• The Exception class also contains two methods which are very useful.– The getMessage() method returns the error

string stored in the class.– The printStackTrace() method prints the list

of methods calls that occurred before the exception.

Throwing an Exception

• Any Java method can throw any of the pre-defined exception classes. – Defined exception classes include:

• NoClassDefFound, OutOfMemoryError, StackOverFlowError, ClassNotFoundException, InstantiationException, NullPointerException, NoSuchMethodException, SecurityException, NumberFormatException, EOFException, FileNotFoundException, ArrayIndexOutOfBoundsException, etc.

Throwing an Exception

• You can also throw an exception that you define. – You throw and catch the exception you

define just as you would a predefined exception.

– An exception you define must be a subclass of the Exception class.

Throwing an Exception

• Defining your own exception

public class CS2Exception extends Exception {

public CS2Exception () {

super(“This is the CS2 Exception. You did

something wrong!!”);

} // end of CS2Exception method

} // end of CS2Exception Class

Throwing an Exception

• To throw an exception you need to use the throw statement in your method.

throw new CS2Exception();

– The place where your program encounters this statement is called the throw point

Throwing an Exception

• If you decide that methodX will throw an exception, then any method that calls methodX must either catch the exception or re-throw it.

Catching Exceptions

• Every exception thrown by a program must be caught in the program. – The exception must be caught in the

method that threw the exception or some other method in the method calling chain.

– If an exception is thrown and not caught by the program, then the program will abort.

Try/Catch Blocks

• A Try/Catch Block is used to catch an exception in your program. – The try section goes around the code that

will potentially throw an exception.• There is only one try block in each try/catch

block combination.

Try/Catch Blocks

– The catch section goes around the code that will potentially catch the exception and handle it.

• There can be multiple catch blocks for each try block.

• If there are multiple catch blocks, each one catches a different exception.

– There may also be a finally block that always executes.

Try/Catch Blocks

• The basic structure is:try {

some executable statements;

}

catch (SomeException e) {

some exception handlers;

}

finally {

some executable statements;

}

Example

• Handling a Divide by Zero exception.

• Propagate User Exception.

The Exception Hierarchy

• The Exception class is the highest class in the hierarchy. It is the parent class of all other exceptions.– It is also the generic exception which will

catch every possible exception.

The Exception Hierarchy• It can become tedious to catch every

possible exception in your program, especially if it is a large program. – The most important exceptions should be caught

separately and then the less important exceptions can be caught using the Exception class.

– Remember to put the individual catch statements first though.

• Why?

Exception Handling

• It is possible that a method may handle an exception but also re-throw the exception.– This is usually done if there is further

processing to be completed by a higher-level catch block.

A Final Example

• NegativeParameterException Class

• ParameterException Class

• ParameterTooLargeException Class

• TestExceptions