1 / 13

Exceptions

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.

loc
Télécharger la présentation

Exceptions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 doesn’t perform the way it intend to

  2. Runtime error • Runtime errors occur for various reason • Invalid input • Open a file which doesn’t 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: try–catch 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 • Java’s 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{ • }

More Related