Exception Handling (10) - COP 3330: Exception Handling In...

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 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

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: COP 3330: Exception Handling In Java Page 1 Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2010 Exception Handling In Java School of Electrical Engineering and Computer Science University of Central Florida Instructor : Dr. Mark Llewellyn markl@cs.ucf.edu HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3330/sum2010 COP 3330: Exception Handling In Java Page 2 Mark Llewellyn Exception Handling In Java When a program encounters a runtime error, it terminates abnormally. What we would like as software developers, is for our programs to either continue execute or else terminate gracefully in the event of a run- time error. In Java an exception is an object created from an exception class. COP 3330: Exception Handling In Java Page 3 Mark Llewellyn Exception Handling In Java To demonstrate exception handling, consider the following code that reads two integers and computes their quotient. What happens when you enter 5 and 2? What happens when you enter 5 and 0? COP 3330: Exception Handling In Java Page 4 Mark Llewellyn Exception Handling In Java Enter 6 and 3no problem! Enter 6 and 0oops! Run-time exception COP 3330: Exception Handling In Java Page 5 Mark Llewellyn Exception Handling In Java A simple fix to the quotient code to fix this problem is shown below. Simple fix is to insert an if statement to make sure that the 2 nd number is not zero. COP 3330: Exception Handling In Java Page 6 Mark Llewellyn Exception Handling In Java COP 3330: Exception Handling In Java Page 7 Mark Llewellyn Exception Handling In Java We can rewrite the quick fix solution to our Quotient program using exception handling. This is shown on the next page. Notice that the exception handling version is somewhat larger than the simple fix version. This example is too small to actually achieve any serious benefit from exception handling and as such we would not typically employ exception handling in this sort of situationthis was an example only. COP 3330: Exception Handling In Java Page 8 Mark Llewellyn try block catch block COP 3330: Exception Handling In Java Page 9 Mark Llewellyn Exception Handling In Java Output from the catch block that caught the thrown exception COP 3330: Exception Handling In Java Page 10 Mark Llewellyn Exception Handling In Java The try block contains the code that is executed in normal circumstances. The catch block contains the code that is executed when an exception occurs. In this case, the program throws an exception by executing throw new ArithmeticException(Divisor cannot be zero); The value thrown, is called an exception . The execution of a throw statement is called throwing an exception ....
View Full Document

This note was uploaded on 07/04/2011 for the course COP 3330 taught by Professor Staff during the Summer '08 term at University of Central Florida.

Page1 / 53

Exception Handling (10) - COP 3330: Exception Handling In...

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