Chapter 3.7.2- try catch| Introduction to Programming Using Java
3.7.2 try catch
When an exception occurs, we say that the exception is “thrown”. For example, we say that Integer.parseInt(str) throws an exception of type NumberFormatException when the value of str is illegal. When an exception is thrown, it is possible to “catch” the exception and prevent it from crashing the program. This is done with a try..catch statement. In somewhat simplified form, the syntax for a try..catch is:
The hexception-class-namei could be NumberFormatException, IllegalArgumentException, or some other exception class. When the computer executes this statement, it executes the statements in the try part.
If no error occurs during the execution of hstatements-1i, then the computer just skips over the catch part and proceeds with the rest of the program. However, if an exception of type hexception-class-namei occurs during the execution of hstatements-1i, the computer immediately jumps to the catch part and executes hstatements-2i, skipping any remaining statements in hstatements-1i. During the execution of hstatements-2i, the hvariablenamei represents the exception object, so that you can, for example, print it out.
At the end of the catch part, the computer proceeds with the rest of the program; the exception has been caught and handled and does not crash the program. Note that only one type of exception is caught; if some other type of exception occurs during the execution of hstatements-1i, it will crash the program as usual.
(By the way, note that the braces, { and }, are part of the syntax of the try..catch statement. They are required even if there is only one statement between the braces. This is different from the other statements we have seen, where the braces around a single statement are optional.)
As an example, suppose that str is a variable of type String whose value might or might not represent a legal real number. Then we could say:
If an error is thrown by the call to Double.parseDouble(str), then the output statement in the try part is skipped, and the statement in the catch part is executed.
It’s not always a good idea to catch exceptions and continue with the program. Often that can just lead to an even bigger mess later on, and it might be better just to let the exception crash the program at the point where it occurs. However, sometimes it’s possible to recover from an error. For example, suppose that we have the enumerated type enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
and we want the user to input a value belonging to this type. TextIO does not know about this type, so we can only read the user’s response as a string. The function Day.valueOf can be used to convert the user’s response to a value of type Day. This will throw an exception of type IllegalArgumentException if the user’s response is not the name of one of the values of type Day, but we can respond to the error easily enough by asking the user to enter another response.
Here is a code segment that does this. (Converting the user’s response to upper case will allow responses such as “Monday” or “monday” in addition to “MONDAY”.)
The break statement will be reached only if the user’s response is acceptable, and so the loop will end only when a legal value has been assigned to weekday.
Read More…
Introduction to Programming Using Java – David J. Eck
Chapter 2 – Names and Things |
8 thoughts on “Chapter 3.7.2- try catch | Introduction to Programming Using Java”