Exceptions in Java a second look - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Exceptions in Java a second look

Description:

Although unchecked exceptions are allowed to be ignored ... Unchecked do not have to be listed here, but should be. Example 9-25-Exc2. Writing your own ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 25
Provided by: pooyash
Category:

less

Transcript and Presenter's Notes

Title: Exceptions in Java a second look


1
Exceptions in Javaa second look
2
Exceptions
  • Definition An exception is an event, which
    occurs during the execution of a program, that
    disrupts the normal flow of the program's
    instructions.
  • Exceptions are Javas way of telling you
    something has gone wrong
  • When an exceptional event occurs, an exception
    object is created storing information about the
    nature of the exception (kind, where it occurred,
    etc.). When this happens, we say that an
    exception is thrown.
  • The JVM looks for a block of code to catch and
    handle the exception (do something with it)?

3
Exceptions
Exceptions are run-time events which indicate an
exceptional situation
int t new int10 for(int i0 ilt100
i) ti
String snull System.out.println(s.length())
int i5 for(int j3jgt-1j--)
System.out.println(i/j)
4
Exceptions
Exceptions are run-time events which indicate an
exceptional situation
int t new int10 for(int i0 ilt100
i) ti
ArrayIndexOutOfBoundsException
String snull System.out.println(s.length())
NullPointerException
int i5 for(int j3jgt-1j--)
System.out.println(i/j)
ArithmeticException
5
Exception Handling
  • The runtime system exhaustively searches the
    methods on the call stack for the exception
    handler (catch block).
  • Should find a handler before main.
  • Main should never throw exceptions!
  • The program will always crash if an exception is
    thrown but not caught before or in main!
  • Examples 2 3 -gt AugEx

6
When an exception is thrown
  • The call stack

7
Another Example
8
Exceptions
methodC() for(int j3jgt-1j--)
System.out.println(i/j)
ArithmeticException Never Caught!
methodB() methodC()
methodA() methodB()
Ignored code
main() methodA()
9
Exception Handling - I
methodC() try for(int
j3jgt-1j--) System.out.println(i/j
) catch(ArithmeticExcept
ion ae) System.out.println(ae)

ArithmeticException
Ignored code
methodB() methodC()
methodA() methodB()
main() methodA()
10
Exception Handling - II
methodC() throws ArithmeticException
for(int j3jgt-1j--)
System.out.println(i/j)
methodB() throws ArithmeticException
methodC()
methodA() try
methodB() catch(ArithmeticException ae)
System.out.println(ae)
Ignored code
main() methodA()
11
Exception Types
  • Checked Exceptions MUST be caught or thrown (by
    the function) for the code to compile. These
    extend Exception
  • Runtime Exceptions do not have to be caught, but
    the program will terminate if they arent! These
    extend RuntimeException.

12
Standard Checked Exceptions
13
Standard Runtime Exceptions
14
Exceptions Hierarchy
15
(No Transcript)
16
Exception Handling
  • It is good practice to handle all exceptions.
  • Although unchecked exceptions are allowed to be
    ignored
  • If they are ignored and an exception is thrown,
    the program will crash!
  • Usually, a method will tell you in the API or in
    the signature which exceptions might be thrown.

17
Exception Handling
  • Exceptions are handled by catch blocks.
  • Must catch the CORRECT Exception.
  • All Exceptions can be caught by using (Exception
    e) Can be used as last check.
  • Should be more descriptive catch the exact
    exception being thrown to handle it correctly!

18
The try catch block
try //code that may throw exception catch(
ExceptionType1 e1) //code to handle
exceptions of type ExceptionType1 or
subclass catch(ExceptionType2 e2) //code
to handle exceptions of type ExceptionType2 or
subclass //more catch blocks if
needed finally //code always to be
executed after try block code
  • Each try must have at least 1 catch
  • A finally block (optional) is always executed
    regardless of what happens in the try

19
Structuring a method
try //code that may throw exception catch(
ExceptionType1 e1) //code to handle
exceptions catch(ExceptionType2 e2) //code
to handle exceptions //more catch blocks if
needed finally //code always to be
executed
doSomething() // code that does not
throw exceptions // try-catch-finally
blocks //code that does not throw
exceptions //try-catch-finally blocks

20
Example
public class Foo public static int divide
(int array, int index) try
System.out.println(Try block)
arrayindex 2 arrayindex/arrayindex1
System.out.println(Done try)
catch(ArithmeticException ae)
System.out.println(Arithmetic exception)
catch(ArrayIndexOutOfBoundsException aioobe)
System.out.println(Index out of
bounds) finally
System.out.println(finally)
System.out.println(Code outside of try)
public static void main(String args)

21
Throwing Exceptions
  • The programmer can generate exceptions to be
    thrown.
  • Throw new ArithmeticException(Divide by zero)
  • Good programming practice!
  • foo throws ArithmeticException Users of the
    function know to catch the exception!!
  • Unchecked do not have to be listed here, but
    should be.
  • Example 9-25-Exc2

22
Writing your own
public class ZeroDivideException extends
Exception public ZeroDivideException()
super() public ZeroDivideException(int
index) super(/ by zero at index
index)
23
Throwing your own
public static int divide (int array, int index)
throws ZeroDivideException try
System.out.println(Try block)
arrayindex 2 arrayindex/arrayindex1
System.out.println(Done try)
catch(ArithmeticException ae)
System.out.println(Arithmetic exception)
throw new ZeroDivideException(index1)
catch(ArrayIndexOutOfBoundsException aioobe)
System.out.println(Index out of bounds)
finally System.out.println(finally)
System.out.println(Code outside of
try)
Write a main() to catch this exception, fix it by
setting array value to 1 and recalling method for
next index.
24
Finally Clause
  • This code is ALWAYS executed even if an
    exception is thrown.
  • Use finally to close input streams or do any kind
    of cleanup that may not occur in the try block.
  • Open file in try check if null in finally and
    then close if not null!
Write a Comment
User Comments (0)
About PowerShow.com