Basic exception handling
Download
1 / 11

Basic Exception Handling Exceptions in Java - PowerPoint PPT Presentation


  • 274 Views
  • Updated On :

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 .

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Basic Exception Handling Exceptions in Java' - Sharon_Dale


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Exceptions in java l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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 l.jpg
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.


ad