10-exceptions

10-exceptions - COMP 202 – Introduction to Computing 1...

Info iconThis preview shows pages 1–7. 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

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

View Full DocumentRight 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: 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 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 – … 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 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 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 COMP 202 – Introduction to Computing 1 COMP 202 - Exceptions 6 Zero.java public class Zero { //----------------------------------------------------------- // Deliberately divides by zero to produce an exception....
View Full Document

This note was uploaded on 12/29/2010 for the course COMP 202 taught by Professor Verbrugge during the Fall '07 term at McGill.

Page1 / 20

10-exceptions - COMP 202 – Introduction to Computing 1...

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

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