I have below class with one method which throws Checked Exception.
public class Sample{
public String getName() throws CustomException{
//Some code
//this method contacts some third party library and that can throw RunTimeExceptions
}
}
CustomException.java
public class CustomException Extends Exception{
//Some code
}
Now in another class i need to call above the method and handle exceptions.
public String getResult() throws Exception{
try{
String result = sample.getName();
//some code
}catch(){
//here i need to handle exceptions
}
return result;
}
My requirement is:
sample.getName()
can throw CustomException and it can also throw RunTimeExceptions
.
In the catch block, I need to catch the exception. If the exception that is caught is RunTimeException
then I need to check if the RunTimeException
is an instance of SomeOtherRunTimeException
. If so, I should throw null instead.
If RunTimeException
is not an instance of SomeOtherRunTimeException
then I simply need to rethrow the same run time exception.
If the caught exception is a CustomException
or any other Checked Exception, then I need to rethrow the same. How can I do that?