Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic."— Presentation transcript:

1 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.

2 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.

3 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.

4 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.

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

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

7 Exception Handling Examples –Divide By Zero –Array Out of Bounds

8 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.

9 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.

10 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.

11 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.

12 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 {...}

13 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.

14 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.

15 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.

16 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.

17 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.

18 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

19 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

20 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.

21 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.

22 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.

23 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.

24 Try/Catch Blocks The basic structure is: try { some executable statements; } catch (SomeException e) { some exception handlers; } finally { some executable statements; }

25 Example Handling a Divide by Zero exception. Propagate User Exception.

26 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.

27 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?

28 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.

29 A Final Example NegativeParameterException Class ParameterException Class ParameterTooLargeException Class TestExceptions


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

Similar presentations


Ads by Google