One of the most effective techniques used to deal with code is to isolate assignments. We learned this with methods. Here are examples: import java.util.Scanner; public class Exercise { private static double getSide() { double side; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); return side; } private static void show(double side) { System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } public static void main(String[] args) { double squareSide = getSide(); show(squareSide); } } Here is an example of running the program: Square Processing Enter Side: 22.50 Square Characteristics Side: 22.50 Perimeter: 90.00 Inside of any method, you write code for exception handling, just as we have done so far. Here is an example: import java.util.Scanner; public class Exercise { private static double getSide() { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); return side; } private static void show(double side) { try { System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } catch(Exception e) { System.out.println("The operation could not be completed!"); } } public static void main(String[] args) { double squareSide = getSide(); show(squareSide); } } Here is an example of running the program: Square Processing Enter Side: 14.55 Square Characteristics Side: 14.55 Perimeter: 58.20 Here is another example of running the same program: Square Processing Enter Side: 2se Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at Exercise.getSide(Exercise.java:11) at Exercise.main(Exercise.java:32) One of the ways you can use methods in exception handling is to have a central method that dispatches assignments to other methods. The other methods can use the values of variables; for example they can test. If an exception occurs, the other method can display a message or throw an exception. This throwing can be picked up by the central method that sent the variable. The method that originated the trying can hand it to a catch block or one of the catch blocks in the calling method that can handle the exception. This means that you can create a method that only throws an exception but does not necessarily deal with it. Here is an example of such a method: import java.util.*; public class Exercise { private static double getSide() { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); if( side < 0 ) throw new InputMismatchException("Positive number required"); return side; } public static void main(String[] args) { } } Notice that the above getSide() method signals that an exception could be thrown and it even specifies the type of exception. If you write this type of method that is only prepared to throw an exception, you must prepare the method that calls it to handle the exception. To do this, in the calling method, create exception handling code and include a catch block that can handle the type of the exception thrown. Here is an example: import java.util.*; public class Exercise { private static double getSide() { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); if( side < 0 ) throw new InputMismatchException("Positive number required"); return side; } private static void show(double side) { System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } public static void main(String[] args) { try { double squareSide = getSide(); show(squareSide); } catch(InputMismatchException exc) { System.out.println(exc.getMessage()); } } }
Notice that in the above program, besides main(), there are two methods; one throws an exception, the other does not. Besides writing code that throws an exception, to indicate to the compiler that a method is throwing an exception, after it closing parenthesis, type the throws keyword followed by the class name of the type of exception that will be thrown. Here is an example: import java.util.*; public class Exercise { private static double getSide() throws InputMismatchException { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); if( side < 0 ) throw new InputMismatchException("Positive number required"); return side; } private static void show(double side) { try { System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } catch(Exception e) { System.out.println("The operation could not be completed!"); } } public static void main(String[] args) { try { double squareSide = getSide(); show(squareSide); } catch(InputMismatchException exc) { System.out.println(exc.getMessage()); } } } In reality, even if a method does not explicitly throw an exception, you can still include a throws InputMismatchException expression to it if you want, in anticipation to a bad behavior. Here is an example: import java.util.*; public class Exercise { private static double getSide() throws InputMismatchException { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); if( side < 0 ) throw new InputMismatchException("Positive number required"); return side; } private static void show(double side) throws InputMismatchException { System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } public static void main(String[] args) { try { double squareSide = getSide(); show(squareSide); } catch(InputMismatchException exc) { System.out.println(exc.getMessage()); } } } In the above code, the throws InputMismatchException expression is option. In reality, there is a rule you must follow. If a method has code that throws a general exception of type Exception, you must add a throws Exception expression to it. If you violate this rule, the program will not compile. For example, the following code will fail: import java.util.*; public class Exercise { private static double getSide() { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); if( side < 0 ) throw new Exception("Positive number required"); return side; } public static void main(String[] args) { } } This would produce: Exercise.java:13: unreported exception java.lang.Exception; must be caught or de clared to be thrown throw new Exception("Positive number required"); ^ 1 error The solution is to include a throws Exception expression to it: import java.util.*; public class Exercise { private static double getSide() throws Exception { double side = 0; Scanner scnr = new Scanner(System.in); System.out.println("Square Processing"); System.out.print("Enter Side: "); side = scnr.nextDouble(); if( side < 0 ) throw new Exception("Positive number required"); return side; } private static void show(double side) throws InputMismatchException { System.out.println("\nSquare Characteristics"); System.out.printf("Side: %.2f\n", side); System.out.printf("Perimeter: %.2f\n", side * 4); } public static void main(String[] args) { try { double squareSide = getSide(); show(squareSide); } catch(Exception exc) { System.out.println(exc.getMessage()); } } }
|
|
||||||||||||
|