• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Exception handling
 

Exception handling

on

  • 2,230 views

Description about Exception Handling

Description about Exception Handling

Statistics

Views

Total Views
2,230
Views on SlideShare
2,116
Embed Views
114

Actions

Likes
1
Downloads
100
Comments
0

2 Embeds 114

http://www.ustudy.in 104
http://ustudy.in 10

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • The first kind of exception is the checked exception . These are exceptional conditions that a well-written application should anticipate and recover from. The second kind of exception is the error . These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. Errors are not subject to the Catch or Specify Requirement. Errors are those exceptions indicated by Error and its subclasses The third kind of exception is the runtime exception . These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API.
  • If you feel that a method doesn't know how to handle a particular error, you can throw an exception from the method and let someone else deal with it. If you throw a "checked" exception, you enlist the help of the Java compiler to force client programmers to deal with the potential exception, either by catching it or declaring it in the throws clause of their methods.

Exception handling Exception handling Presentation Transcript

  • Exception Handling Presentation on Presented By : Sumit Lole
  • Agenda
    • What is Exception?
    • Exception Hierarchy
    • Exception vs. Error
    • Types of Exception
    • Default Exception Handler
    • Exception Handling
    • Creating Your own Exception
  • Exception
    • Definition:  
    • “ An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions”
    • An exception occurs when our code asks the JVM to perform the technically impossible and unanticipated (by the compiler)!
  • Exception Hierarchy
    • All exception types are subclasses of the built-in class Throwable
    • Throwable has two subclasses :
      • Exception
      • Error
  • Throwable Error Exception Runtime Exception IO Exception The Java Exception Hierarchy
  • Error
    • Error hierarchy describes internal errors and resource exhaustion.
    • Don’t throw an object of this type!
    • Relatively rare
    • Mostly beyond programmers control
  • Types of Exceptions in Java
    • Checked exceptions
      • All exception inherited from Exception Class
    • Unchecked exceptions
      • All exception inherited from RuntimeException Class
  • Default Exception Handler ?
    • Default handler prints a stack trace from the point at which the exception occurred, and terminates the program
    • Example: BasicDemo.java
  • Exception Handling
    • Anticipate the error by the user/system.
    • Return the program to a safe state that enables the user to execute other commands.
    • Inform the user of the error’s cause.
    • Allow the user to save work and terminate the program gracefully.
  • Ways to throw an Exception
    • Calling a method that throws a checked exception
    • Code detects an error and generates checked exception with throw statement
    • Programming error e.g. a[-1] =0; generates
    • an unchecked exception ArrayIndexOutOfBoundsException
    • 4. JVM or runtime library internal error
  • Keywords for Java Exceptions
    • throws
    • throw
    • try
    • catch
    • finally
  • Catching Exceptions
    • “ What goes up must come down”
    • Catching exceptions is a bit trickier
    • An uncaught exception will terminate the program …
    • … print the exception type to screen …
    • … print the stack trace.
  • Using try and catch
    • Placed the code (which is suppose to throw an exception) inside a block of code starting with the “ try ” keyword
    • The catch clause should follow immediately the try block.
    • Once the catch statement has executed, program control continues with the next line in the program following the entire try/catch mechanism
    • Example : InitialDemo.java
  • Nested Try Statement
    • A try statement can be inside the block of another try
    • Each time a try statement is entered, the context of that exception is pushed on the stack
    • If an inner try statement does not have a catch, then the next try statement’s catch handlers are inspected for a match
    • If a method call within a try block has try block within it, then then it is still nested try
    • Example : ExceptionChaining.java
  • What information does java.lang.Exception contain
    • The type of exception -- the exception class
    • Where the exception occurred -- the stack trace
    • Context and explanatory information -- the error message, and other state information
    • Example : StackTrace.java
  • finally
    • It is used to handle premature execution of a method (i.e. a method open a file upon entry and closes it upon exit)
    • finally creates a block of code that will be executed after try/catch block has completed and before the code following the try/catch block
    • finally clause will execute whether or not an exception is thrown
  • Some Important Exception that generally occurs…
    • IOException
    • ClassNotFoundException
    • RuntimeException
      • Most occurring exception
      • Caused by programming error
      • “ If it’s a RuntimeException it’s your fault!”
      • Example :
          • ArrayIndexOutOfBoundException
          • IndexOutOfBound
          • ArithmeticException
          • NullPointerException
          • Example : ExceptionExample.java
  • throw
    • It is possible for your program to throw an exception explicitly
    • throw ExceptionInstance
    • Here, ExceptionInstance must be an object of type Throwable or a subclass Throwable
    • There are two ways to obtain a Throwable objects:
      • Using a parameter into a catch clause
      • Creating one with the new operator
  • Creating Your Own Exceptions
    • Java provides over 40 categories of Exceptions
    • Java allows you to create your own Exceptions
      • Extend a subclass of Throwable
    • Exception class constructors
      • Exception()
      • Exception(String message)
      • Exception(String message, Throwable cause)
      • Exception(Throwable cause)
  • Example
    • throw statement :
    • throw <object reference expression>;
    • Class DivisionByZeroException extends Exception{
    • DivisionByZeroException(String msg) {
    • super(msg);
    • }
    • }
    • public void division(){
    • int num1 = 10;
    • int num2 = 0;
    • if(num2 == 0)
    • throw new DivisionByZeroException();
    • System.out.println(num1/num2);
    • }
  • throws
    • If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception
      • type method-name parameter-list) throws exception-list
      • {
      • // body of method
      • }
    • It is not applicable for Error or RuntimeException, or any of their subclasses
  • Rule for Method Overriding
    • While overriding, the new method definition in the subclass can only specify all or a subset of the exception classes specified in the throws clause of the overridden method in the superclass
  • Some Methods Inherited from Throwable
    • fillStackTrace() returns Throwable
    • getCause() returns Throwable
    • getMessage() returns String
    • getStackTrace() return StackTraceElement[] - Throwable
    • initCause(Throwable)
    • printSatckTrace() return void
    • setStackTrace(Throwable)
    • toString() return String
  • Queries