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";
}
}