Exceptions - PowerPoint PPT Presentation

About This Presentation
Title:

Exceptions

Description:

Uncaught Exceptions. int a = 5; int b = 0; int c = a/b; ... at ExceptionsExamples.main(ExceptionsExamples.java:12) Uncaught Exceptions. Call stack trace ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 10
Provided by: csUs7
Learn more at: https://www.cs.usfca.edu
Category:

less

Transcript and Presenter's Notes

Title: Exceptions


1
Exceptions
2
Overview
  • An exception is an object that defines an
    unusual or erroneous situation.
  • Examples
  • Divide by 0
  • Array index out of bounds
  • File cannot be found
  • Follow a null reference

3
Uncaught Exceptions
  • int a 5
  • int b 0
  • int c a/b
  • Exception in thread "main" java.lang.ArithmeticExc
    eption / by zero
  • at ExceptionsExamples.main(ExceptionsExamples.java
    12)

4
Uncaught Exceptions
  • Call stack trace
  • info about where the exception occurred
  • info about all methods that were called to get to
    the method where the exception occurred
  • Exception in thread "main" java.lang.ArithmeticEx
    ception / by zero
  • at ExceptionsExamples.caller2(ExceptionsExamples.j
    ava20)
  • at ExceptionsExamples.caller1(ExceptionsExamples.j
    ava12)
  • at ExceptionsExamples.main(ExceptionsExamples.java
    8)

5
try/catch
  • try
  • //statements that may throw an exception
  • catch(Exception_Type name)
  • //what to do in case of exception
    //Exception_Type
  • catch(Another_Type another_name)
  • //what to do in case of exception Another_Type

6
Propagation
  • void divider() throws ArithmeticException
  • int a 5
  • int b 0
  • int c a/b

7
Exception Class Hierarchy
  • Throwable
  • Error
  • Exception
  • IOException
  • EOFException, FileNotFoundException
  • ClassNotFoundException
  • RuntimeException
  • ArithmeticException
  • ClassCastException
  • NullPointerException

8
Designing/Throwing Own Exception
  • Create a class that extends Exception
  • OutOfRangeException
  • if out of range
  • throw new OutOfRangeException(message)

9
Exercise
  • Write a program to process a configuration file.
Write a Comment
User Comments (0)
About PowerShow.com