Reputation: 309
I've created my own exception but when I try to use it I receive a message saying that it can't be cast to my exception
I've got one interface like this
public interface MyInterface
{
public OtherClass generate(ClassTwo two, ClassThree three) throws RetryException;
}
other like this one
public class MyGenerator{
public class generate (ClassTwo two, ClassThree three){
try{
}catch(MyException my)
}
}
and finally a method in another class
public Object evaluate(String expression, Map values) throws FirstException, RetryException
{
try{
}catch (Exception x){
if(x instanceof FirstException){
throw new FirstException()
}
else{
RetryException retry= (RetryException)x;
retry.expression = expression;
retry.position = position;
retry.operator = tokens[position];
retry.operand = 1;
throw retry;
}
}
}
This try catch block on the last method is to make maths operation and I want to catch a division by zero exception on the RetryException
.
Upvotes: 5
Views: 211
Reputation: 24262
I am going to make some big assumptions here as to what is happening, since the code examples are very incomplete.
In your evaluate method, you are getting an ArithmeticException due to a divide by zero and you want to handle it by throwing your own RetryException in the handler. The exception received cannot be casted since it is of the wrong type, you should catch the ArithmeticException in your evaluate method and then create and throw a new RetryException.
public Object evaluate(String expression, Map values) throws FirstException, RetryException
{
try{
// Some code that may throw FirstException
int x = 10/0;
}catch (ArithmeticException x){ // Handle divide by zero
RetryException retry= new RetryException();
retry.setExpression(expression);
retry.setPosition(position);
retry.setOperator(tokens[position]);
retry.setOperand(1);
throw retry;
}
}
}
This all assumes that an appropriate RetryException exists with the appropriate setter methods of course.
FirstException catch was removed since it was hiding the real stacktrace by creating a new instance when none is required. It is already declared in the method signature and there was no actual handling.
Upvotes: 4
Reputation: 10325
RetryException retry= (RetryException)x;
This line of code is attempting to cast an Exception as a RetryException. This will only work if: RetryException appropriately extends the Exception type that you are catching (ArithmeticException for divide by zero, I think?). AND the Exception actually IS a RetryException. Without looking at more of your logic, we have no idea if this is true.
Try checking
if (x instanceof RetryException)
Before you do this cast. Your code may be throwing a different kind of Exception.
Preferably, you would instead have multiple catch blocks...
try{
//}catch (FirstException e) -- I removed this, as you are only catching it and then directly
// throwing it, which seems uneecessary
}catch (RetryException r){
//process r
throw r;
}
If I misunderstood your question, I'll do my best to correct this.
Upvotes: 6