Basic Exception Handling - PowerPoint PPT Presentation

About This Presentation
Title:

Basic Exception Handling

Description:

This is called throwing an exception. Another part of your code contains code ... way of handling exceptions in Java consists of the try-throw-catch threesome. ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 12
Provided by: robertw8
Learn more at: https://www.ecs.csun.edu
Category:

less

Transcript and Presenter's Notes

Title: Basic Exception Handling


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("E
    nter number of donuts") donutCount
    keyboard.nextInt( ) System.out.println("E
    nter number of glasses of milk")
    milkCount keyboard.nextInt( ) if
    (milkCount lt 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(milkCoun
    t " 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 (contd)
  • 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("En
    ter number of donuts") donutCount
    keyboard.nextInt( )
    System.out.println("Enter number of glasses of
    milk") milkCount
    keyboard.nextInt( ) if (milkCount lt
    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 (contd)
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com