{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exception Handling (10) - COP 3330 Object-Oriented...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
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 [email protected] HEC 236, 407-823-2790 http://www.cs.ucf.edu/courses/cop3330/sum2010
Background image of page 1

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

View Full Document Right Arrow Icon
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.
Background image of page 2
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?
Background image of page 3

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

View Full Document Right Arrow Icon
COP 3330: Exception Handling In Java Page 4 © Mark Llewellyn Exception Handling In Java Enter 6 and 3…no problem! Enter 6 and 0…oops! Run-time exception
Background image of page 4
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.
Background image of page 5

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

View Full Document Right Arrow Icon
COP 3330: Exception Handling In Java Page 6 © Mark Llewellyn Exception Handling In Java
Background image of page 6
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 situation…this was an example only.
Background image of page 7

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

View Full Document Right Arrow Icon
COP 3330: Exception Handling In Java Page 8 © Mark Llewellyn try block catch block
Background image of page 8
COP 3330: Exception Handling In Java Page 9 © Mark Llewellyn Exception Handling In Java Output from the catch block that caught the thrown exception
Background image of page 9

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

View Full Document Right Arrow Icon
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 . The exception is an object created from an exception class. In this case, the exception class is java.lang.ArithmeticException .
Background image of page 10
COP 3330: Exception Handling In Java Page 11 © Mark Llewellyn Exception Handling In Java When an exception is thrown, the normal execution flow is interrupted.
Background image of page 11

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

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

{[ snackBarMessage ]}