CSC245 Lecture 7
CSC245 Lecture 7
Although this form of error handling works, intermixing program logic with error-handling logic can make
programs difficult to read, modify, maintain and debug—especially in large applications.
Exception handling enables programmers to remove error-handling code from the “main line” of the
program’s execution, improving program clarity and enhancing modifiability.
Result: 100 / 7 = 14
Result: 100 / 7 = 14
Result: 100 / 7 = 14
Exception: java.util.InputMismatchException
You must enter integers. Please try again.
Result: 100 / 7 = 14
An uncaught exception is an exception that occurs for which there are no matching catch blocks.
Causes a program to terminate, if the program has only one thread.
Terminates a only the thread where the exception occurred, if a program has multiple threads. There
may be adverse effects to the rest of the program.
Error prevention tip: If you know that a method might throw an exception, include appropriate exception-
handling code in your program to make it more robust.
Exceptions are not required to be listed in a method’s throws clause -- even if they are, it is not required
that such exceptions be caught by an application.
Method throwException
Exception handled in method throwException
Finally executed in throwException
Exception handled in main
Method doesNotThrowException
Finally executed in doesNotThrowException
End of method doesNotThrowException