lect14exceptions_4up - CMPT 126: Lecture 11 Exceptions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT 126: Lecture 11 Exceptions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University November 22, 2007 1 Exception Handling Certain problems is a Java program may generate exceptions or errors. An exception is an object that defines unusual or invalid processing and alters the normal flow of execution. An exception is thrown by a program or run-time environment and can be caught and handled appropriately by switching execution to a predefined handler. An error is similar to an exception except that it represents an unrecoverable situation and should not be caught. Java has a predefined set of exceptions and errors that may occur during the execution of a programs. CMPT 126: Exceptions, Lecture 11 2 Problems represented by exceptions and errors A few example problems handles by exceptions and errors include: Attempting to divide by zero (a mathematical singularity) An out of bounds array index A specified file that could not be found A requested I/O operation that could not be completed normally An attempt was made to follow a null reference An attempt was made to execute an operation that violates some kind of security measure. Some of these situations represent a truly erroneous situation, and some merely represent an exceptional situation that doesnt occur under usual conditions. Exception handling deals with the latter. CMPT 126: Exceptions, Lecture 11 3 Processing an exception A program can be designed to process an exception in one of three ways: ignore the exception (uncaught exceptions) handle the exception where it occurs, or handle the exception at another point in the program CMPT 126: Exceptions, Lecture 11 4 Uncaught Exceptions The following program deliberately divides by zero to produce an ArithmeticException. public class Zero { public static void main(String args) { int numerator = 10; int denominator = 0; System.out.println(numerator/denominator); System.out.println("This line will not be reached."); } } If an exception is not handled (as it is not in this case), the program will terminate abnormally and produce a message that describes what exception occurred and where it was produced. CMPT 126: Exceptions, Lecture 11 5 Exception Error Message The following message is provided when the Zero program terminates abnormally: Output Exception in thread "main" java.lang.ArithmeticException: / by zero at Zero.main(Zero.java:7) This message provides information that can help us track down the problem. The first line of the exception output indicates which exception was thrown and provides some information about why it was thrown....
View Full Document

Page1 / 7

lect14exceptions_4up - CMPT 126: Lecture 11 Exceptions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online