Detecting Errors and Exception Handlers

We have seen a multitude of error messages (everyone makes programming mistakes) and we have devised error trapping procedures to prevent users from sabotaging (crashing) a program by entering the wrong input. It's time to expand our error trapping skills.

Dealing with Errors:

An exception is an error that affects program execution. If an exception is not handled (dealt with), the program terminates. You most likely have seen the term "exception" in your past programming endeavors. If you asked a user to enter an integer (that would be stored in an int), and the user entered 2.4, the message "Exception in thread "main" java.util.InputMismatchException" appeared in red in the console as the program stopped execution.

In certain cases, an "exception handler" may be used to provide the user with an error message as to what went wrong, before the program abruptly ends. It may also enable the program to deal with exceptional situations and continue its normal execution.

definition An exception handler is a block of code that performs an action when an exception occurs.

You have already been dealing with potential errors when you "error trapped" your programs to guarantee that your user-input complied with your directions. Consider this simple program to demonstrate integer division, where the problem of division by zero could crash the program with the error message Exception in thread "main" java.lang.ArithmeticException: / by zero at Errors.main(Errors.java:11):

errortest0

By including the if/else error trapping statements, the programmer has dealt with the situation of the user mistakenly entering the second number as zero. Instead of the program crashing, the program simply prints a message regarding division by zero (no crash). Problem avoided!

Exception handling is a more sophisticated process of dealing with potential errors in more complex programs. It is program code that deals with the anticipated exception and allows the program to continue after dealing with the exception. Let's look at a more sophisticated version of error trapping our division by zero problem.

erortest2bb

Console Display:
error2pic


exception handler format:
try {
     statement(s)
}
catch (exception err_code) {
     statement(s)

}
The try statement(s) addresses the situation(s) that could possibly cause an exception.

The catch waits for an exception that matches the exception err_code to occur and then executes its statement(s).

NOTE: We knew the exception err_code in the example above was Arithmetic Exception because we have seen it appear in the error message when division by zero actually occurs.
Exception in thread "main" java.lang.ArithmeticException: / by zero at Errors.main(Errors.java:11)
:


dividerdash

Before we leave this section, let's take a look at the first coding problem mentioned on this page where a user was asked to enter an integer, but the user fails to do so. It would be extremely difficult to check to see if the value a user entered would be an integer if you attempted the check using if/else statements. But, an exception handler may just save the day. Take a look.
"Exception in thread "main" java.util.InputMismatchException"

userint
dividerdash

There are certain methods that will "throw" an exception (error) under certain circumstances. If you place your mouse on the method in your program code and hit F2, you will see if that method will throw exceptions and which ones it will throw.
The methods printStackTrace() and e.getmessage() can be used to help you find and understand which exceptions are occurring. You will see these methods placed under catch in upcoming programs.

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com

Notice: These materials are free for your on-line use at this site, but are not free for the taking.
Please do not copy these materials or re-post them to the Internet, as it is copyright infringement.
If you wish hard-copies of these materials, refer to our subscription area, JavaMathBits.com.
Help us keep these resources free. Thank you.