EXCEPTION HANDLING
[ Try Catch Throw Finally ]
By
Sanjay.K
Saran.S
Somansankara. P.R
Vishnuprasanth.B
AGENDA:
• Java exception keywords
• Internal working of try -catch
• Throw
• Throws
• Finally
Catch
• Each catch block is an exception handler that handles the type of
exception indicated by its argument.
• The catch block contains code that is executed if and when the
exception handler is invoked.
• The runtime system invokes the exception handler when the handler
is the first one in the call stack whose ExceptionType matches the
type of the exception thrown
Throw
• Throw keyword explicitly throw an exception.
• The flow of execution of the program stops immediately after the throw statement is executed and
the nearest enclosing try block is checked to see if it has a catch statement that matches the type of
exception.
• If it finds a match, controlled is transferred to that statement otherwise next enclosing try block is
checked and so on. If no matching catch is found then the default exception handler will halt the
program.
Throws
• Throws is a keyword in Java which is used in the signature of method to indicate that this method
might throw one of the listed type exceptions.
• The caller to these methods has to handle the exception using a try-catch block.
• In a program, if there is a chance of raising an exception then compiler always warn us about it and
compulsorily we should handle that checked exception, Otherwise we will get compile time error
saying unreported exception XXX must be caught or declared to be thrown. To prevent this
compile time error we can handle the exception in two ways:
• By using try catch
• By using throws keyword
• We can use throws keyword to delegate the responsibility of exception handling to the caller (It
may be a method or JVM) then caller method is responsible to handle that exception.
Finally
• Java finally block is a block used to execute important code such as closing the connection, etc.
• Java finally block is always executed whether an exception is handled or not. Therefore, it contains
all the necessary statements that need to be printed regardless of the exception occurs or not.
• The finally block follows the try-catch block.
Module 4-1.pptx

Module 4-1.pptx

  • 1.
    EXCEPTION HANDLING [ TryCatch Throw Finally ] By Sanjay.K Saran.S Somansankara. P.R Vishnuprasanth.B
  • 2.
    AGENDA: • Java exceptionkeywords • Internal working of try -catch • Throw • Throws • Finally
  • 7.
    Catch • Each catchblock is an exception handler that handles the type of exception indicated by its argument. • The catch block contains code that is executed if and when the exception handler is invoked. • The runtime system invokes the exception handler when the handler is the first one in the call stack whose ExceptionType matches the type of the exception thrown
  • 8.
    Throw • Throw keywordexplicitly throw an exception. • The flow of execution of the program stops immediately after the throw statement is executed and the nearest enclosing try block is checked to see if it has a catch statement that matches the type of exception. • If it finds a match, controlled is transferred to that statement otherwise next enclosing try block is checked and so on. If no matching catch is found then the default exception handler will halt the program.
  • 9.
    Throws • Throws isa keyword in Java which is used in the signature of method to indicate that this method might throw one of the listed type exceptions. • The caller to these methods has to handle the exception using a try-catch block. • In a program, if there is a chance of raising an exception then compiler always warn us about it and compulsorily we should handle that checked exception, Otherwise we will get compile time error saying unreported exception XXX must be caught or declared to be thrown. To prevent this compile time error we can handle the exception in two ways: • By using try catch • By using throws keyword • We can use throws keyword to delegate the responsibility of exception handling to the caller (It may be a method or JVM) then caller method is responsible to handle that exception.
  • 10.
    Finally • Java finallyblock is a block used to execute important code such as closing the connection, etc. • Java finally block is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not. • The finally block follows the try-catch block.