Thursday, October 1, 2015

Java exception handling and creating custom exception


Below example shows how to use Java lag predefined exceptions in two ways as throws in method signature and using try catch cause.
This example also shows how to create your own exception/ user defined exception



 package ExceptionHandler;  
 import java.io.File;  
 import java.io.IOException;  
 public class ExceptionTutorial {  
      public static void main(String[] args) {  
           try {  
                new ExceptionTutorial().TestFileException();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
           new ExceptionTutorial().TestFileException1();  
           new TestCustomExceptions().checkMobilePhoneName("ddd");  
      }  
      /*** Example about Java in built exception class ***/  
      /*  
       * Method "TestFileException" is throwing IOException and try catch block on  
       * main method where "TestFileException" method's object is created is  
       * catching the IO exception  
       */  
      public void TestFileException() throws IOException {  
           File file = new File("C:/does_not_exist");  
           file.createNewFile();  
      }  
      public void TestFileException1() {  
           File file = new File("C:/does_not_exist");  
           try {  
                // here I am using the createNewFile method, this method throws  
                // IOException - If an I/O error occurred.  
                // the catch block is catching the IO exception  
                file.createNewFile();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  
 }  
 /*** Example about Java custom exception / user defined exception ***/  
 /*  
  * This exercise is to built a custom exception class Use the custom exception  
  * class in your method and throw the exception  
  *   
  */  
 class UserException extends RuntimeException {  
      public UserException(String message) {  
           super(message);  
      }  
 }  
 // create a method which will throw the custom exception created  
 class TestCustomExceptions {  
      public void checkMobilePhoneName(String name) throws UserException {  
           if (name.contentEquals("apple")) {  
                System.out.println("This is a iPhone");  
           } else {  
                throw new UserException("Wrong phone");  
           }  
      }  
 }  

OUTPUT
java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at ExceptionHandler.ExceptionTutorial.TestFileException(ExceptionTutorial.java:31)
at ExceptionHandler.ExceptionTutorial.main(ExceptionTutorial.java:11)
java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at ExceptionHandler.ExceptionTutorial.TestFileException1(ExceptionTutorial.java:40)
at ExceptionHandler.ExceptionTutorial.main(ExceptionTutorial.java:17)

Exception in thread "main" ExceptionHandler.UserException: Wrong phone
at ExceptionHandler.TestCustomExceptions.checkMobilePhoneName(ExceptionTutorial.java:68)
at ExceptionHandler.ExceptionTutorial.main(ExceptionTutorial.java:18)


This is an example to show how to continue with the program execution after exception is raised. Using for loop.


 package Robo.RoboFramework;  
 public class DisplayName {  
      public static void main(String[] args) {  
           new DisplayName().tryCatch();  
           /*  
            * try { new DisplayName().tryCatch(); } catch (CustomException e) { //  
            * TODO Auto-generated catch block e.printStackTrace(); }  
            */  
      }  
      // A method throwing an exception  
      String cityName[] = { "Pune", "pune", "Delhi" };  
      public void displayCityName(String cityName) throws CustomException {  
           System.out.println("City Name : " + cityName);  
           if (!cityName.equals("Pune")) {  
                throw new CustomException("This is not Pune");  
           } else if (cityName == "Pune") {  
                System.out.println("This is Pune");  
           }  
      }  
      // using try catch to catch the exception and continue with execution  
      // using the try catch inside the for loop  
      public void tryCatch() {  
           int i;  
           for (i = 0; i < cityName.length; i++) {  
                // continue;  
                try {  
                     new DisplayName().displayCityName(cityName[i]);  
                } catch (CustomException c) {  
                     System.out.println("EXCEPTION RAISED");  
                }  
           }  
      }  
      class CustomException extends RuntimeException {  
           public CustomException(String message) {  
                super(message);  
           }  
      }  
 }  


OUTPUT
City Name : Pune
This is Pune
City Name : pune
EXCEPTION RAISED
City Name : Delhi
EXCEPTION RAISED


No comments:

Post a Comment