bob
bob

Reputation: 135

Java Custom Exceptions

I am trying to catch an exception, and then throw it.

try{
    arr = new class[arrLen]; //Goes to a constructor that may cause an exception
}
catch(Exception e){
    MyException e = new MyException;
    throw e;
}

As you can see, I am trying to execute a command, and then if it causes an exception, I want to create a new instance of MyException and then throw it.

What I am having problems with is whether I am supposed to create the "Exception e" and then try and create a new "MyException e", or am I supposed to make a new MyException, AKA

MyException f = new MyException;
throw f;

Upvotes: 0

Views: 79

Answers (1)

disrvptor
disrvptor

Reputation: 1612

You should create a new MyException with a new variable, like f. This new exception should use the original Exception as the cause. For example

MyException f = new MyException(e);
throw f;

Or you can reuse the e variable if MyException extends Exception.

e = new MyException(e);
throw e;

Upvotes: 1

Related Questions