1

I have problem with custom exceptions. I'd like to throw and catch my own exception if standard exception was caught. But it's impossible when I try to throw my own exception in catch block. Can you tell me how to do it properly? :(

SampleClass.java:

public class SampleClass {

    int[] arr = new int[6];

    public void fillArray() {
        for (int i = 0; i < 6; i++) {
            arr[i] = i;
        }
    }

    public void getElement(int index) {
        try {
            System.out.println(arr[index]);
        } catch (IndexOutOfBoundsException ex) {
            throw new MyIoobEx();
        } catch (MyIoobEx e) {
            e.getMessage();
        }
    }

    public static void main(String[] args) {

    }
}

MyIoobEx.java:

    public class MyIoobEx extends IndexOutOfBoundsException {
        @Override
        public String getMessage() {
            return "Bad index given";
        }
}
padrian92
  • 147
  • 4
  • 16
  • 2
    The `catch` statements that follow a `try` statement only act on whatever happened in that `try`. If you rethrow in the `catch`, that's a completely other context. You'll need another `try-catch` around the whole thing. – Savior Apr 18 '16 at 19:30
  • 2
    @Pillar please make that an answer. – Louis Wasserman Apr 18 '16 at 19:30

3 Answers3

2

If you want to throw another Exception, you have to surround the whole logic with another try-catch, so as to catch the nested Exception that you are throwing.

public void getElement(int index) {

      try {           

            try {
                System.out.println(arr[index]);
            } catch (IndexOutOfBoundsException ex) {
                throw new MyIoobEx();
            } catch (MyIoobEx e) {
                e.getMessage();
            }

        } catch (MyIoobEx e) {
             e.getMessage();
     }
}
Swayam
  • 16,294
  • 14
  • 64
  • 102
1

You're throwing a new error up the chain. A try-catch statement can't "catch" errors it's throwing, you'll need to put another try{} around the whole thing.

Daniel Paczuski Bak
  • 3,720
  • 8
  • 32
  • 78
1

If you want to throw your own exception after catching one, you will need to surround the catching block with another try block. Another way would be casting your own exception into the caught Exception class... but I don't know if it would serve your purpose.

You can either do this:

try{
    try{
        throw new Exception("original exception");
    }
    catch(Exception e){
        throw new CustomException();
    }
}
catch(CustomException e){
   //overrides original exception
}

...or you can do this:

try{
    throw new Exception("original exception");
}
catch(Exception e){
    //you should do some query on what kind of exception caught here
    e = new CustomException("new exception");
}    

Generally though, I don't know why would you even do this in the first place. Seems like an extra step to me.

daedsidog
  • 1,732
  • 2
  • 17
  • 36
  • You mean public void getElement(int index) throws MyIoobEx { //content} ? – padrian92 Apr 18 '16 at 20:06
  • No, just `e = new YourExceptionClass()`. You can do what you said, but that would require another try/catch block around your existing try/catch blocks. – daedsidog Apr 18 '16 at 20:14