Exceptions: When things go wrong - PowerPoint PPT Presentation

About This Presentation
Title:

Exceptions: When things go wrong

Description:

Exceptions: When things go wrong Topics to be covered: What is an exception Throwing an exception Catching an exception – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 17
Provided by: Rebecc196
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Exceptions: When things go wrong


1
Exceptions When things go wrong
  • Topics to be covered
  • What is an exception
  • Throwing an exception
  • Catching an exception

2
Expecting the unexpected
  • When you design a program, you develop an
    algorithm for whats supposed to happen but, as
    we all know, life doesnt always work that way.
    Example
  • The user enters a floating point number instead
    of a integer
  • You try to evaluate X / Y, when Y has the value 0
  • You try to access position 10 in an array with 10
    elements

3
Example
  • public class Exception1
  • public static void main(String args)
  • int x 1, y 0
  • int z x/y
  • System.out.println("Finished!")
  • Screen Output
  • Exception in thread main java.lang.ArithmeticExc
    eption / by zero
  • at Exception1.main(Exception1.java7)

4
Exceptions are objects
  • When something goes wrong we need to do something
    about it
  • Exceptions are objects that contain information
    about what went wrong and where it happened
  • The getMessage() method returns a string
    explaining the exception
  • Exception in thread main java.lang.ArithmeticExc
    eption / by zero
  • The printStackTrace() method prints the call
    stack trace
  • at Exception1.main(Exception1.java7)

5
Exception class hierarchy
Object
Throwable
Error
Exception
RunTimeException
ArithmeticException
IndexOutOfBoundsException
You can define your own exceptions
6
Creating your own exception
  • public class MyException extends Exception
  • public MyException(String message)
  • super(message)

7
Another (silly) example
  • public class Exception2
  • public static void main(String args)
  • int x 1, y 0
  • System.out.println("Starting the
    calculations")
  • int ans calculations(x,y)
  • System.out.println("The answer is " ans)
  • public static int calculations(int x, int y)
  • int ans division(x,y)
  • return ans
  • public static int division(int x, int y)
  • int z x/y
  • return z

8
Normal control flow
main()
calculations()
division()
Execution begins in main() which invokes
calculations() which then invokes division().
When division() finishes control returns to
calculations() and then from there to main().
9
When an exception is generated
main()
calculations()
division()
OH NO!
When a method has a problem, it generates an
exception object.
It then throws the exception back along the call
path.
The exception travels along the call path until
it is caught by a compatible catch statement.
The catch statement may exist in the method where
the exception was first generated or in any
other method on the call path.
10
The throw clause
  • A method must declare that it may throw an
    exception
  • This information is as much a part of its normal
    header as its return type or its parameters
  • public static int division(int x, int y) throws
    MyException
  • The information lets anyone calling the method
    know that they must be prepared for it to throw
    this exception
  • A throw statement is a little like a return
    statement except that a method may throw more
    than one exception and each exception may be of a
    different type

11
Throwing an exception
  • public class Exception2
  • public static void main(String args) throws
    MyException
  • int x 1, y 0
  • System.out.println("Starting the
    calculations")
  • int ans calculations(x,y)
  • System.out.println("The answer is " ans)
  • public static int calculations(int x, int y)
    throws MyException
  • int ans division(x,y)
  • return ans
  • public static int division(int x, int y) throws
    MyException
  • int z 0
  • if(y0) MyException exceptionObj
  • new MyException("Division would be
    undefined")
  • throw exceptionObj
  • else
  • z x/y
  • return z

12
The screen output
  • Starting the calculations
  • Exception in thread "main" MyException Division
    would be undefined
  • at Exception2.division(Exception2.java19)
  • at Exception2.calculations(Exception2.java
    12)
  • at Exception2.main(Exception2.java7)
  • Press any key to continue...

13
Catching an exception
  • If a throw statement is like return statement,
    then a try/catch statement is a little like an
    if/else statement.
  • Statements that may throw an exception must be
    enclosed in a try block---this is just a regular
    block of code preceded by the key word try
  • The try block is followed by a catch block, which
    gives the instructions to execute if an exception
    of the specified type is caught
  • The catch clause always has a single parameter
    specifying the exception to be caught
  • The catch body is executed only if an exception
    of the appropriate type is thrown

14
A try/catch example
  • public class Exception2
  • public static int calculations(int x, int y)
  • public static int division(int x, int y)
  • int z 0
  • try
  • if(y0) MyException eObj new

    MyException("Division would be undefined")
  • throw eObj
  • else
  • z x/y
  • catch(MyException e)
  • System.out.println("The exception
    message is " e.getMessage())
  • return z

15
The screen output
  • Starting the calculations
  • The exception message is Division would be
    undefined
  • The answer is 0
  • Press any key to continue...

16
Points to remember
  • The try body contains statements that may throw
    an exception at least one statement in the block
    must potentially throw an exception
  • Each catch block has a parameter that defines the
    type of exception it can catch
  • If a method catches an exception, it stops the
    propagation of that exception up the call path.
    Methods with appropriate try/catch blocks do not
    have throw declarations in their headers.

public static int division(int x, int y) throws
MyException
NO
Write a Comment
User Comments (0)
About PowerShow.com