110 likes | 425 Views
Basic Exception Handling. Exceptions in Java. Either the Java language itself or your code provides a mechanism that signals when something unusual happens. This is called throwing an exception . Another part of your code contains code for handling the exception .
E N D
Exceptions in Java • Either the Java language itself or your code provides a mechanism that signals when something unusual happens. • This is called throwing an exception. • Another part of your code contains code for handling the exception.
Example of Exception Handling import java.util.*;public class GotMilk{ public static void main(String[] args) { int donutCount, milkCount; double donutsPerGlass; Scanner keyboard = new Scanner(System.in); System.out.println("Enter number of donuts:"); donutCount = keyboard.nextInt( ); System.out.println("Enter number of glasses of milk:"); milkCount = keyboard.nextInt( ); if (milkCount < 1) { System.out.println("No Milk!"); System.out.println("Go buy some milk."); } else { donutsPerGlass = donutCount/(double)milkCount; System.out.println(donutCount + " donuts."); System.out.println(milkCount + " glasses of milk."); System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk."); } System.out.println("End of program."); }}
Example of Exception Handling (cont’d) import java.util.*;public class ExceptionDemo{ public static void main(String[] args) { int donutCount, milkCount; double donutsPerGlass; Scanner keyboard = new Scanner(System.in); try { System.out.println("Enter number of donuts:"); donutCount = keyboard.nextInt( ); System.out.println("Enter number of glasses of milk:"); milkCount = keyboard.nextInt( ); if (milkCount < 1) throw new Exception("Exception: No Milk!"); donutsPerGlass = donutCount/(double)milkCount; System.out.println(donutCount + " donuts."); System.out.println(milkCount + " glasses of milk."); System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk."); } catch(Exception e) { System.out.println(e.getMessage( )); System.out.println("Go buy some milk."); } System.out.println("End of program."); }}
How Exception Handling Works • Exception is a predefined class. • The throw statement creates an object of the class Exception and throws it. • When an exception is thrown, the code in the surrounding try block stops execution, and another portion of code, know as a catch block, begins execution. • Execution of the catch block is called catching the exception.
The try Block • The basic way of handling exceptions in Java consists of the try-throw-catch threesome. • A try block contains the code for the basic algorithm that tells the computer what to do when everything goes smoothly. • It is called try because you are not 100 percent sure everything will go smoothly.
The throw Statement • If something goes wrong, you want to throw an exception. • The throw statement creates a new object of the class Exception and throws it. • When an exception is thrown, the code in the try block stops execution, and another portion of code, the catch block, begins execution.
The throw Statement (cont’d) • In the example, new Exception(“Exception: No Milk!”) the string “Exception: No Milk!” is an argument for the constructor for the class Exception. • The Exception object, created with new, stores this string in an instance variable of the object, so that it can be recovered in the catch block.
The catch block • Although it is not a method definition it looks a little like one. • It is a separate piece of code that is executed when a program throws an exception. • The throw statement is similar to a method call, but instead of calling a method, it calls the catch block.
The catch Block Parameter • The following the word catch is the catch-block parameter. • The class name preceding the catch-block parameter specifies what kind of exception the catch-block can catch. • The catch-block parameter gives you a name for the exception that is caught, so that you can write code in the catch block in order to manipulate that exception object.
The getMessage Method • Every exception has a String instance variable that contains some message, which typically identifies the reason for the exception. • If the object is called e, then the method call e.getMessage() returns this string.