210 likes | 218 Views
Learn about exceptions, throwing & catching them, multiple catch blocks, finally block, creating custom exceptions, important methods, and exercises in Java programming.
E N D
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 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.
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
CPIT 305 Advance Programming edit-compile-run cycle Interpreter Compiler Editor ( bytecode file) ( source file) Running Program
CPIT 305 Advance Programming Classification of Exception IndexOutOfBoundException NullPointerException Error Throwable Exception ArithmeticException EOFException FileNotFoundException AssertionError VirtualMachineError RuntimeException ClassNotFoundException IOException InputMismatchException IllegalArgumentException NumberFormatException
CPIT 305 Advance Programming Exceptions Types:
CPIT 305 Advance Programming Example1 (Unchecked Exception): • Practical example
CPIT 305 Advance Programming Example2 (Checked Exception): • Practical example
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 )
CPIT 305 Advance Programming Exercise 1 • Write a program in java that read numbers from text file and print its square into console.
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.
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; }
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.
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.
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.
CPIT 305 Advance Programming Important methods • The Throwable class contains three useful methods: • void printStackTrace(); • string getMessage(); • string toString();
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.
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();
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.
CPIT 305 Advance Programming Important Keywords • Try • Catch • Finally • Throw • Throws