An exception is an unusual situation that could occur in your program. As a programmer, you should anticipate any abnormal behavior that could be caused by the user entering wrong information that could otherwise lead to unpredictable results. The ability to deal with a program’s abnormal behavior is called exception handling. Java provides many keywords and built-in classes to handle an exception. You proceed with a top-down approach. To start handling an exception, create a section of code that includes the normal flow of the program. This section starts with the try keyword followed by an opening curly bracket and a closing curly bracket. The section in the curly brackets is the body. In it, you write the normal code. Here is an example: import java.util.Scanner; public class Exercise { public static void main(String[] args) { double side; Scanner scnr = new Scanner(System.in); try { System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } } } Just after the try section, that is, after the closing curly bracket of try, you create a new section that uses the catch keyword. This new section uses the following formula: catch(ExceptionClass) { WhatToDo } As you can see, this section starts with the catch keyword with parentheses. The parentheses behave like those of a method. You must pass an argument. At a minimum, you can pass a class named Exception and a name for the argument. After the closing the parenthesis, create a body for the section. Like a normal body, this starts with an opening curly bracket and ends with a closing curly bracket. Based on this, the basic formula of exception handling is:
Here is an example of a program that handles an exception: import java.util.Scanner; public class Exercise { public static void main(String[] args) { double side = 0; Scanner scnr = new Scanner(System.in); try { System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } catch(Exception e) { } } } Here is an example of running the program: Square Processing Enter Side: 22.98 Square Characteristics Side: 22.98 Perimeter: 91.92 This is another example of running the same program: Square Processing Enter Side: Yes
As mentioned already, if an error occurs when processing the program in the try section, the compiler transfers the processing to the next catch section. You can then use the catch section to deal with the error. At a minimum, you can display a message to inform the user. To display a message resulting from an exception, create it in the catch block. Here is an example: import java.util.Scanner; public class Exercise { public static void main(String[] args) { double side = 0; Scanner scnr = new Scanner(System.in); try { System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } catch(Exception exc) { System.out.println("The value you provided for the side is invalid"); } } } Here is an example of running the program: Square Processing Enter Side: 24 Square Characteristics Side: 24.00 Perimeter: 96.00 Here is another example of running the program: Square Processing Enter Side: 26GW The value you provided for the side is invalid You can provide any message you want. The idea is to be as help and explicit as possible so the user may know what the problem was.
|
|
||||||||||||||||
|