throw exception in java


Contents of page :
  • About throw keyword >
  • throw unchecked exception >
  • throw checked exception >
  • If checked Exception is not handled either by try-catch or throws, we will face compilation error.
  • Program 1- Handling Exception in try-catch block where it was thrown.
  • Program 2- Handling Exception by throwing it from m() method (using throws keyword) and handling it in try-catch block from where call to method m() was made.
  • Program 3- Throwing Exception from m() method and then again throwing it from calling method [ i.e. main method]


About throw keyword >


throw unchecked exception >
  • We need not to handle unChecked exception either by catching it or throwing it.

We throw NullPointerException (unChecked exception) and didn’t handled it, no compilation error was thrown.


throw checked exception >
  • We need to handle checked exception either by catching it, or
  • throwing it by using throws keyword. (When thrown, exception must be handled in calling environment)


If checked Exception is not handled either by try-catch or throws, we will face compilation error.

Program 1- Handling Exception in try-catch block where it was thrown.
import java.io.FileNotFoundException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionTest {
   public static void main(String[] args) {
          m();
          System.out.println("after calling m()");
   }
   static void m(){
          try {
                throw new FileNotFoundException();
          } catch (FileNotFoundException e) {
                System.out.println("FileNotFoundException handled in try-catch block");
          }
   }
}
/*OUTPUT
FileNotFoundException handled in try-catch block
after calling m()

*/
We throwed FileNotFoundException (checked exception) by using throw keyword and handled it in try-catch block.




Program 2- Handling Exception by throwing it from m() method (using throws keyword) and handling it in try-catch block from where call to method m() was made.
import java.io.FileNotFoundException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionTest {
   public static void main(String[] args) {
          try {
                 m();
          } catch (FileNotFoundException e) {
             System.out.println("FileNotFoundException handled in try-catch block");
          }
          System.out.println("after calling m()");
   }
   static void m() throws FileNotFoundException{
          throw new FileNotFoundException();
   }
}
/*OUTPUT
FileNotFoundException handled in try-catch block
after calling m()
*/
method m() propagated exception to calling method (i.e. main method) using throws.


Program 3- Throwing Exception from m() method and then again throwing it from calling method [ i.e. main method]
Ultimately exception is not handled properly in this case, but this approach is used in many live projects (i.e. in web applications).
package throwChecked_3_throw_throw;
import java.io.FileNotFoundException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ExceptionTest {
   public static void main(String[] args) throws FileNotFoundException {
          m();
          System.out.println("after calling m()");
   }
   static void m() throws FileNotFoundException{
          throw new FileNotFoundException();
   }
}
/*OUTPUT
Exception in thread "main" java.io.FileNotFoundException
   at throwChecked_3_throw_throw.ExceptionTest.m(ExceptionTest.java:12)
   at throwChecked_3_throw_throw.ExceptionTest.main(ExceptionTest.java:8)
*/
Please note that System.out.println("after calling m()") statement wasn't executed.

method m() propagated exception to calling method (i.e. main method)  using throws, and
main propagated exception to JVM using throws.





RELATED LINKS>





No comments:

Post a Comment