1 / 11

Basic Exception Handling

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 .

Sharon_Dale
Télécharger la présentation

Basic Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Exception Handling

  2. 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.

  3. 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."); }}

  4. 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."); }}

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

More Related