1 / 21

Exception Handling

CPIT 305 Advance Programming. Exception Handling. CPIT 305 Advance Programming. Outline: What is an Exception ? Edit - Compile - Run Cycle Classification of Exception Exception Types Multiple Catch Blocks The Finally block Important Methods Create Custom Exception class. CPIT 305

carlathomas
Télécharger la présentation

Exception Handling

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. CPIT 305 Advance Programming Exception Handling

  2. CPIT 305 Advance Programming Outline: • What is an Exception ? • Edit - Compile - Run Cycle • Classification of Exception • Exception Types • Multiple Catch Blocks • The Finally block • Important Methods • Create Custom Exception class

  3. CPIT 305 Advance Programming What is an Exception ? An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions. When an exception occurs, we say an exception is thrown. When the matching exception-handling code is executed, we say the thrown exception is caught.

  4. CPIT 305 Advance Programming What is an Exception ? Throwing Exceptions: When the exception occur inside specific method it should be throw via one of the following statements: - throw new IOException(); - IOException ex = new IOException(); throw ex; Declaring Exceptions: When the method doesn’t contain try catch blocks to handle the exception, it should propagate it using throws clause - Public void method() throws IOException ( Catching Exceptions: In order to handle the exceptions in the right way, should use try-catch blocks Throws the exception Forward the exception method without exception handler method where exception occur Propagator method Catch the exception Catcher method method with exception handler main method

  5. CPIT 305 Advance Programming edit-compile-run cycle Interpreter Compiler Editor ( bytecode file) ( source file) Running Program

  6. CPIT 305 Advance Programming Classification of Exception IndexOutOfBoundException NullPointerException Error Throwable Exception ArithmeticException EOFException FileNotFoundException AssertionError VirtualMachineError RuntimeException ClassNotFoundException IOException InputMismatchException IllegalArgumentException NumberFormatException

  7. CPIT 305 Advance Programming Exceptions Types:

  8. CPIT 305 Advance Programming Example1 (Unchecked Exception): • Practical example

  9. CPIT 305 Advance Programming Example2 (Checked Exception): • Practical example

  10. CPIT 305 Advance Programming Multiple Catch Blocks It’s possible to have multiple catch blocks with a single try block. But, keep in mind the exception hierarchy when writing multiple catch clauses ( Subclass exception must caught beforeSuperclass exception )

  11. CPIT 305 Advance Programming Exercise 1 • Write a program in java that read numbers from text file and print its square into console.

  12. CPIT 305 Advance Programming Exercise 2 • Write a Java program that read integer number entered by user (this integer number should be between 0 to 100), • then square this number and print it into console. • If entered number is not integer then print the exception.

  13. CPIT 305 Advance Programming The Finally block • Block of code that needs to be executed regardless of whether an exception is thrown or not. try { Statement 1; } catch (Exception e){ Statement 2; } finally { Statement 3; }

  14. CPIT 305 Advance Programming Remember: • Resources should be released in the finally block if exception blocks have been used in your code. • Releasing resources in the finally block is preferred because the finally block is guaranteed to be executed whether an exception has occurred or not.

  15. CPIT 305 Advance Programming throws clause • If method is not interested in handling the exception then it can throw back the exception to the caller method using throws keyword. • Any exception that is thrown out of a method must be specified as such by a throws clause.

  16. CPIT 305 Advance Programming Example 3 • Practical example • The class contains two methods namely method1 & method2 and one main method. • The main method will make call to method1 and then method1 will call method2. • The method2 contains the file reading code.

  17. CPIT 305 Advance Programming Important methods • The Throwable class contains three useful methods: • void printStackTrace(); • string getMessage(); • string toString();

  18. CPIT 305 Advance Programming Important methods • printStackTrace() • It’s method defined in the Throwable class – a super class of Exception and Error classes. • It used to show the full method calling history with line numbers. • Extremely useful in debugging, • very useful tool for diagnosing an Exception, It tells you what happened and where in the code this happened.

  19. CPIT 305 Advance Programming Create custom Exception class • Should be extends from Exception class • Public Class ExceptionNameextends Exception { • We can throw it explicitly in the line where we want the custom exception to be thrown. • throw new ExceptionName();

  20. CPIT 305 Advance Programming Exercise 3 • Write a java program that read number entered by user through input dialog (this number should be between 0 to 100), • then square this number and print it into console • If entered number is not between 0 and 100 then throw an exception.

  21. CPIT 305 Advance Programming Important Keywords • Try • Catch • Finally • Throw • Throws

More Related