Exceptions

Exceptions - COE318 Fall 2008 Olivia Das Elec and Comp Engg...

Info iconThis preview shows pages 1–11. 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 Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 1 Exceptions COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 2 Exception • An exception is an object that defines an unusual or erroneous situation. • can be caught and handled if desired • An error is similar to an exception except that an error generally represents an unrecoverable situation and should not be caught. COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 3 Examples of situations that cause exceptions to be thrown • attempting to “divide by zero” • accessing an out-of-bound array element • a specified file not found • trying to invoke a method on null reference COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 4 How to deal with Exceptions ? 3 ways: • do not handle the exception at all • handle the exceptions where it occurs • handle the exception at another point in the program COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 5 Advantage of Exception Model • Most important advantage is: Separating Error Handling Code from "Regular" Code (we will see that in a moment…) COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 6 Uncaught Exceptions • if a program does not handle the exception at all, then: • it will terminate abnormally • produce a message that describes what exception occurred and where it originated COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 7 Uncaught Exceptions public class DivideByZeroTest { public static void main(String args) { int a = 100; int b = 0; int c = divide ( a, b ); System.out.println( “Answer is: ” + c); } public static int divide ( int i, int j ) { return i / j ; } } Output ?? COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 8 Output of DivideByZero program java DivideByZero Exception in thread "main" java.lang.ArithmeticException: / by zero at DivideByZeroTest.divide(DivideByZero.java:11) at DivideByZeroTest.main(DivideByZero.java:6) COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 9 Catching and Handling Exceptions (where it occurs) public class DivideByZeroTest { …… public static int divide ( int i, int j ) { int k = 0; try { k = i / j ; } catch( ArithmeticException ex ) { System.out.println( “Caught exception.” ); } return k; } } COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 10 public class DivideByZeroTest { public static void main(String args) { int a = 100; int b = 0; int c = divide (a, b); System.out.println( “Answer is: ” + c); } public static int divide ( int i, int j ) { int k = 0; try { k = i / j ; } catch( ArithmeticException ex ) { System.out.println( “Caught exception.” ); } return k; } } Output: Caught exception....
View Full Document

{[ snackBarMessage ]}

Page1 / 65

Exceptions - COE318 Fall 2008 Olivia Das Elec and Comp Engg...

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

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