Exceptions - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Exceptions

Description:

IOException. AWTException. RuntimeException. ArithmeticException ... public void myMethod() throws IOException. If the method throws multiple exceptions ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 14
Provided by: PROFE172
Category:

less

Transcript and Presenter's Notes

Title: Exceptions


1
Exceptions
  • Three categories of errors
  • Syntax errors
  • Runtime errors
  • Logic errors
  • Syntax errors rules of the language have not
    been followed.
  • Runtime error program is running and environment
    detects an operation that is impossible to carry
    out.
  • Logic errors the program doesnt perform the way
    it intend to

2
Runtime error
  • Runtime errors occur for various reason
  • Invalid input
  • Open a file which doesnt exist
  • Access out-of-bounds array
  • Runtime errors cause exceptions
  • Exception are handled differently from the events
    of GUI programming
  • A event may be ignored in GUI programming, but an
    exception cannot be ignored.
  • Java provides programmers with the capability to
    elegantly handle runtime errors.

3
An example
  • import javax.swing.JOptionPAne
  • public class test
  • public static void main(String args)
  • String input JOptionPane.showInputDialog(null
    , Please enter an integer)
  • int number Integer.parseInt(input)
  • JOptionPane.shoeMessageDialog(null, the number
    entered is number)
  • // if you entered a floating-point value, the
    program terminates

4
Catch this error trycatch block
  • import javax.swing.JOptionPAne
  • public class test
  • public static void main(String args)
  • try
  • String input JOptionPane.showInputDialog
    (null, Please enter an integer)
  • int number Integer.parseInt(input)
  • JOptionPane.shoeMessageDialog(null, the
    number entered is number)
  • catch Exception ex)
  • JOptionPane.showMessageDialog(null,
    Incorrect input an integer is required)

5
Exception classes
  • A Java exception is an instance of a class
    derived from Throwable.
  • The Throwable class is contained in the java.lang
    package
  • Subclasses of Throwable are contained in various
    packages
  • Errors related to GUI components are included in
    the java.awt package
  • Numeric exceptions are included in the java.lang
    package
  • You can create your own exception classes by
    extending Throwable or a subclass of Throwable.

6
Exception classes
  • The exception classes can be classified into
    three types
  • System errors are thrown by the JVM and
    represented in the Error class. The Error class
    describe internal system errors
  • Exceptions are represented in the Exception
    class, which describe errors caused by your
    program and by external circumstance
  • Runtime exceptions are represented in the
    RuntimeException class, which describes
    programming errors, such as bad casting,
    accessing an out-of bounds array, and numeric
    errors.

7
Exception classes and subclasses
  • Object
  • Throwable
  • Exception
  • ClassNotFoundException
  • IOException
  • AWTException
  • RuntimeException
  • ArithmeticException
  • NullPointerException
  • IndexOutOfBoundsException
  • Error
  • LinkageError
  • VirtualMachineError
  • AWTError

8
Unchecked Exceptions
  • RuntimeException, Error and their subclasses
  • Reflect programming logic errors
  • Not recoverable
  • Should be corrected in the program
  • Can occur anywhere
  • Java does not mandate that you write code to
    catch or declare
  • For example, a NullPointerException is thrown if
    you access an object through a reference variable
    before an object is assigned to it

9
Checked exception
  • All other exception are known as checked
    exception
  • The compiler forced the programmer to check and
    deal with them

10
Exception handling
  • Javas exception-handling model is based on three
    operations
  • Declaring an exception
  • Throwing an exception
  • Catching an exception
  • Every method must state the types of checked
    exceptions it might throw
  • Java doesnot require that you declare Error and
    RuntimeException.

11
Declaring exception
  • Other exception thrown by the method must be
    explicitly declared
  • public void myMethod() throws IOException
  • If the method throws multiple exceptions
  • public void myMehtod() throws Exception1,
    Exception2, , ExceptionN

12
Throwing Exceptions
  • Suppose the argument must be non-negative, but a
    negative argument is passed, the program can
    created an instance of IllegalArgumentException
    and throw it
  • IllegalArgumentException ex new
    IllegalArgumentException(Wrong Argument)
  • throw ex
  • Or
  • throw new IllegalArgumentException(Wrong
    Argument)

13
Catching Exceptions
  • try
  • statements
  • catch (Exception1 e1)
  • catch (Exception2 e2)
  • finally
Write a Comment
User Comments (0)
About PowerShow.com