10-exceptions - COMP 202 Exceptions COMP 202 Introduction...

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

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 1 COMP 202 Exceptions CONTENTS : Exceptions and Errors The try-catch statement The try-catch-finally statement Exception propagation
Image of page 1

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

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 2 Exceptions • An exception is an object that describes an unusual or erroneous situation – division by zero – reading the wrong data type from a Scanner – accessing a non existing array-element out of bound – accessing a null object – …
Image of page 2
COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 3 Exceptions When such an unusual situation occurs the program throws an exception it does not continue with the next statement in the program so far, the program actually terminates Instead of letting the program terminate an exception can be caught and handled by another part of the program that is, the programmer writes special code that is executed whenever an exception is thrown A program can therefore be separated into a normal execution flow and an exception execution flow An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught
Image of page 3

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

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 4 Exceptions Exceptions: java.lang.ArrayIndexOutOfBoundsException java.lang.StringIndexOutOfBoundsException java.lang.NullPointerException Errors: java.lang.OutOfMemoryError java.lang.ClassFormatError java.lang.InternalError java.lang.VirtualMachineError
Image of page 4
COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 5 Exception Handling If an exception is ignored by the program, the program will terminate and produce an appropriate message The message includes a call stack trace that indicates on which line the exception occurred The call stack trace also shows the method call trail that lead to the execution of the offending line
Image of page 5

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

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 6 Zero.java public class Zero { //----------------------------------------------------------- // Deliberately divides by zero to produce an exception.
Image of page 6
Image of page 7
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