Wednesday 19 August 2015

Custom Exceptions

Checked Exception
1. Checked exception extends Exception class.

2. Throws/try-catch mandatory to handle exception at compile time in case of checked exception i.e. compilation error unhandled exception type.

// 1. Checked exception extends Exception class.
public class MyCustomException extends Exception {

       private static final long serialVersionUID = 1L;

       public MyCustomException() {
       }

       public MyCustomException(String message) {
              super(message);
       }

       public MyCustomException(Throwable cause) {
              super(cause);
       }

       public MyCustomException(String message, Throwable cause) {
              super(messagecause);
       }

       public MyCustomException(String message, Throwable cause,
                     boolean enableSuppressionboolean writableStackTrace) {
              super(messagecauseenableSuppressionwritableStackTrace);
       }
}

public class CustomExceptionTest {
       public static void main(String[] args) {
              try {
                     testException(null);
              } catch (MyCustomException e) {
                     e.printStackTrace();
              }
       }

// 2. Throws/try-catch mandatory in case of checked exception.
            Otherwise it will throw an unhandled exception type
            MyCustomException at compile time.

       public static void testException(String str)throws MyCustomException {
              if(str == null) {
                     throw new MyCustomException("The String is null");
              }
       }
}

Unchecked Exception

1. Checked exception extends RuntimeException class.

2. No need of Throws/try-catch at compile i.e. there is no compilation error without handling.

// 1. Unchecked exception extends RuntimeException class.
public class MyCustomException extends RuntimeException {

       private static final long serialVersionUID = 1L;

       public MyCustomException() {
       }

       public MyCustomException(String message) {
              super(message);
       }

       public MyCustomException(Throwable cause) {
              super(cause);
       }

       public MyCustomException(String message, Throwable cause) {
              super(messagecause);
       }

       public MyCustomException(String message, Throwable cause,
                     boolean enableSuppressionboolean writableStackTrace) {
              super(messagecauseenableSuppressionwritableStackTrace);
       }
}

public class CustomExceptionTest {
       public static void main(String[] args) {
              try {
                     testException(null);
              } catch (MyCustomException e) {
                     e.printStackTrace();
              }
       }

// 2. Throws/try-catch is not mandatory at compile time.

       public static void testException(String string) {
              if(string == null) {
                     throw new MyCustomException("The String is null");
              }
       }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...