lect14exceptions_4up - Exception Handling CMPT 126 Lecture...

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

View Full Document Right Arrow Icon
CMPT 126: Lecture 11 Exceptions Tamara Smyth, [email protected] 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 doesn’t 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
Image of page 1

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

View Full Document Right Arrow Icon
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. The line(s) following the first line is/are the call stack trace and indicate where the exception occurred. In this case, the stack has only one line: at Zero.main(Zero.java:7) CMPT 126: Exceptions, Lecture 11 6 Call Stack Trace In many cases there will be more than a single line so as to indicate the methods that were called to get to the method that produced the exception.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern