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
.
Disadvantages. Using exceptions for error handling has two disadvantages. First, exceptions can trap only runtime errors. Therefore, a PL/SQL program cannot trap and recover from compile-time (syntax and semantic) errors such as table or view does not exist.
The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.
Errors are conditions that cannot get recovered by any handling techniques. It surely causes termination of the program abnormally. Errors belong to unchecked type and mostly occur at runtime. Some of the examples of errors are Out of memory errors or System crash errors. Example 1 Run-time Error.
Example: Exception handling using try... In the example, we are trying to divide a number by 0 . Here, this code generates an exception. To handle the exception, we have put the code, 5 / 0 inside the try block. Now when an exception occurs, the rest of the code inside the try block is skipped.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With