1

my class extends which class please give me suggestion

public class MyException extends Exception {

    public MyException (Exception ex) {
        super(ex);
    }

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

    public MyException (Exception ex,String moduleKey) {
        super(ex, moduleKey);
    }

     public MyException (Exception ex, String moduleKey, String message) {
        super(ex, moduleKey, message);
    }

!-- end snippet -->

Chandu D
  • 505
  • 3
  • 17

1 Answers1

1

It all depends on whether you want to force API users to try/catch (or declare throws) every time your exception might be raised, or you want them to crash the application just like NullPointerException does.

The first kind should be used whenever the exception is to be expected - like IOExceptions for instance. RuntimeExceptions usually suggest that something weird happend during (as the name suggests) runtime - a weird language behaviour, unexpected thread clash and so on. Usually you want to use the regular Exception though.

Marandil
  • 1,042
  • 1
  • 15
  • 31
  • then what happen if i use throwable as my super class exception – Lakshmi Prasanna Aug 07 '15 at 09:49
  • k please explain some example Marandil – Chandu D Aug 07 '15 at 09:50
  • @ChanduD Imagine two different of exceptions: 1. InvalidPasswordException thrown, when you try to access a resource, but the password provided to access it was actually wrong. You don't want this to crash the application, you want to force the programmer to properly handle the exception whenever a method that can throw it is called (e.g. whenever password is input) 2. IndexOutOfBounds thrown by a list implementation. Forcing the programmer catch that error every time he uses get accessor, he would either declare throws everywhere or just try/catch whole functions defying the purpose. – Marandil Aug 07 '15 at 10:29