how to uncatch the caught exceptions
I want to my java script to throw if there is any exception. Problem is, we are extending a super class in my script but in that super class there is an exception caught called ScriptFailureException
and s开发者_StackOverflow中文版uper class is in a JAR that we cannot edit. We want to stop that exception from being caught. Is it possible to prevent that exception from being caught or is there any other method to make our script fail in this situation. I tried System.Exit()
, etc. methods but they are not working. We are running our script through TestNG.
First of all, Java is not a scripting language and has no notion of a script. You should not use the term script, but the terms "class", "method", "program".
To answer your question, if a method catches an exception, then the exception is caught, and you can't do anything about it. You might throw another exception type which would not be caught by the superclass method, though.
No unless the super class lets you know that a ScriptFailureException
occured by an other way than throwing it. One of this other way can be for example a null returned value for a method call.
What actions does the Jar code take when it catches the exception? Can you detect that action after the event? For instance, if the Jar code writes to a log then you could examine the log to see if that exception is recorded there and then throw a new exception of your own.
You can make your Java fail totally by throwing an Error.
throw new Error("Oh dear");
That's messy though.
Try defining your own Exception and throwing that.
public class CustomException extends Exception { }
// somewhere else
throw new CustomException("Some detail");
A little trick is declare ScriptFailureException
as an extension of RuntimeException
I think that this isn't the best way to do code, but it works. The RuntimeException
objects don't need a explicit try/catch block.
Then, you must declare class StcriptFailureException extends RuntimeException
Regards!
精彩评论