I have a basic question in Java:
I have two methods: functionA & functionB. functionA calls functionB, and functionB rise an exception. The call to functionB is in try scope of functionA.
Now I also want that functionA will go to it catch scope.
There is any way to do that?
If an exception is thrown in methodB and you catch it, one way to propagate it to methodA is to rethrow it:
void methodB() throws SomeException {
try {
//Something that can throw SomeException
} catch (SomeException e) {
//rethrow e
throw e;
}
}
void methodA() {
try {
methodB();
} catch (SomeException e) {
//this block will run if methodB throws SomeException
}
}
But if you need that, you probably should not catch the exception in methodB at all and just let it propagate automatically to methodA:
void methodB() throws SomeException {
//Something that can throw SomeException: don't catch it
}
void methodA() {
try {
methodB();
} catch (SomeException e) {
//this block will run if methodB throws SomeException
}
}
Actually, that is how it works usually, provided that functionB doesn't catch the exception itself. Exceptions, when thrown, bubble up the call stack until a matching catch block is found.
Not sure if this is what you want
void functionB() throws MyException {
try{
.....
}Catch(MyException e){
//do something here;
throw e;
}
}
void functionA() {
try {
functionB();
} catch (MYException e) {
//do something else to A here
}
}
Related
Let's say we have nested methods A, B and C like below:
public void A(){
try{
B();
}
catch(Exception e){
log.error(e);
}
}
public void B(){
C();
}
public void C(){
try{
some_stuff();
}
catch(Exception e){
log.error(e)
}
}
Since I catch the exception at C() and handle it with only logging, and there isn't any catch in B(), do I catch the exception at A()?
Or should I throw the exception at C() and add a try catch at B() to be able to handle it in A()?
When you do
public void C(){
try{
some_stuff();
}
catch(Exception e){
log.error(e)
}
}
This will handle the exception at this point. You will log it and disregard the exception. If you want to log and propagate it up, you need to re-throw it from the catch clause.
Since Exception is checked (as far as I remember) you need to change the method signature to indicate that you are throwing this from C.
At B you don't need to handle it, you can just change the method signature to indicate the method throws Exception.
Read more on Java checked vs unchecked exceptions for example here enter link description here
Finally if you want to propagate to A the code will look like this:
public void A(){
try{
B();
}
catch(Exception e){
log.error(e);
}
}
public void B() throws Exception{
C();
}
public void C() throws Exception{
try{
some_stuff();
}
catch(Exception e){
log.error(e)
throw e;
}
}
You are handling the exception in the catch block, therefore it will not be further propagated to the caller. This is independent of whether you are logging it or doing anything else with it. If you want want to propagate it to the caller, you would need to re-throw it:
[...]
catch (Exception e) {
// do something
throw e;
}
How you handle exceptions depends on the design of your code.
If I have multiple functions in a function which are throwing exceptions what's the best way of handling them if they depend on each other?
With depending on each other I mean that if something throws an exceptions the code after the function which threw the exception should be skipped.
I figured out three ways to do this:
Exception nesting
public void parent() {
someFunction();
}
public void someFunction() {
try {
function1();
try {
function2();
...
} catch (Func2xception e) {
System.out.println("Function 2 failed!");
}
} catch (Func1Exception e) {
System.out.println("Function 1 failed!");
}
}
Return on exception
public void parent() {
someFunction();
}
public void someFunction() {
try {
function1();
} catch (Func1Exception e) {
System.out.println("Function 1 failed!");
return;
}
try {
function2();
} catch (Func2xception e) {
System.out.println("Function 2 failed!");
return;
}
...
}
Add exceptions to method signature
public void parent() {
try {
someFunction();
} catch (Func1Exception e) {
System.out.println("Function 1 failed!");
} catch (Func2Exception e) {
System.out.println("Function 2 failed!");
} ...
}
public void someFunction() throws Func1Exception, Func2Exception {
function1();
function2();
...
}
I sometimes use all of them together and that's a mess. Are there any good practices on how to handle situations like this?
The way to use depends on whether the exceptions should be handled by the client of the someFunction() method or else caught and handled as soon as they happen, that is, inside the someFunction() method.
In the exception nesting case, the nesting is not required.
You can use a single try statement and place the two calls that may generate the exceptions in it.
If an exception occurs in one of the two invoked methods, you finish in one of the catch statements and so the second method is executed only if the first one has not thrown the caught exception.
It produces exactly the same result than your code but with a single try and without nesting that is less readable.
public void someFunction() {
try {
function1();
function2();
...
} catch (Func1Exception e) {
System.out.println("Function 1 failed!");
}
catch (Func2xception e) {
System.out.println("Function 2 failed!");
}
}
This way of doing is suitable if you have some other instructions after the catch statements and you want them to be executed even if one of the expected exceptions was caught.
The return on exception case manifests a close enough problem.
It may be refactored with a single try :
public void someFunction() {
try {
function1();
function2();
...
} catch (Func1Exception e) {
System.out.println("Function 1 failed!");
return;
}
catch (Func2xception e) {
System.out.println("Function 2 failed!");
return;
}
}
...
}
This way of doing is suitable if you have some other instructions after the catch statements and you don't want them to be executed if one of the expected exceptions was caught.
Nevertheless for these two cases, if the exception handling is the same for the two exceptions (Func1Exception and Func2xception), you could group them in a single catch statement :
public void someFunction() {
try {
function1();
function2();
...
}
catch (Func1Exception | Func2xception e) {
System.out.println("Function 1 or 2 failed!");
}
}
At last, the add exceptions to method signature case makes sense only if the exceptions should be handled by the client of the method.
You can catch multiple exceptions in one catch clause, starting from Java 7 I believe.
try {
...
} catch(IOException | IllegalArgumentException | SomeOtherException e) {
...
}
Why this code doesn't print "d". Why it doesn't go to the catch block for RunTimeException?
public static void main(String[] args) {
System.out.println("a");
try {
System.out.println("b");
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
System.out.println("c");
throw new RuntimeException();
}catch (RuntimeException e) {
System.out.println("d");
throw new RuntimeException();
}finally{
System.out.println("e");
throw new RuntimeException();
}
}
The output of this program is
a
b
c
e
Exception in thread "main" java.lang.RuntimeException
EDIT: After throwing IllegalArgumentException it goes to the corresponding catch block and prints 'c'. after that since we don't catch the exception in RuntimeException it doesn't go any further. but since it's guranteed that we go to the finally block it print 'e' and after that throw RunttimeException. if the code was like something like the following, it would throw out the RuntimeException("2"). If we comment the exception inside finally, it throw out RuntimeException("1").
public static void main(String[] args) throws InterruptedException {
System.out.println("a");
try {
System.out.println("b");
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
System.out.println("c");
throw new RuntimeException("1");
}catch (RuntimeException e) {
System.out.println("d");
throw new RuntimeException();
}finally{
System.out.println("e");
throw new RuntimeException("2");
}
}
The reason of this code not printing d is, because in the IllegalArgumentException catch block, after printing "c" and before throwing RuntimeException it executes finally (that's how the flow works). Finally block throws exception itself, so It never gets to throw that RuntimeException that gets it to "d".
public static void main(String[] args) {
System.out.println("a");
try {
System.out.println("b"); // 1
throw new IllegalArgumentException(); // 2
} catch (IllegalArgumentException e) {
System.out.println("c"); // 3
throw new RuntimeException(); // nope, there's a finally block that executes first
}catch (RuntimeException e) {
System.out.println("d");
throw new RuntimeException();
}finally{
System.out.println("e"); // 4
throw new RuntimeException(); // 5 - rest of the code never got executed.
}
}
Hope this was clear enough.
Also, as it was pointed out, even if the RuntimeException after "c" was executed, It would not invoke the lower catch block. The catch block is only invoked once, depending on the Exception thrown in the try block (though this isn't really relevant because the first explanation dictates the flow of your thread).
The catch blocks are NOT in the scope of the try. Any code in the catch blocks executes in the context of the outer main code, and thus has no exception handlers. When you throw RuntimeException the finally block for the try gets executed and then the exception terminates the program.
In try catch block, if one of the catch block catches the exception the other catches doesn't get involved. remeber that finally block always run at the end no matter exception gets threw or not.
I have a code that invokes an external API via EJB and that API occasionally leaks an exception that is not part of the client kit, therefore resulting in ClassNotFoundException.
I have a try-catch block surrounding the call:
try {
thirdPartyLibrary.finalInvokeMethod();
} catch (SomeException exception) {
//Do something
} catch(
..
} catch (Exception exception) {
if (exception instanceof ClassNotFoundException) {
log.error("....");
}
}
I want to avoid using instanceof in catch, but if I add a separate catch clause for ClassNotFoundException, the compiler produces an error "Unreachable catch block", since thirdPartyLibrary.finalInvokeMethod(); doesn't throw ClassNotFoundException.
Is there a better way to address the issue?
I've found a workaround. I've wrapped the thirdPartyLibrary.finalInvokeMethod(); in another method that throws the checked exception. So I got a dedicated catch clause without a compiler error.
private someMethod() {
try {
callExternalAPI();
} catch (SomeException exception) {
//Do something
} catch(
..
} catch (ClassNotFoundException exception) {
log.error("....");
//Do something
} catch (Exception exception) {
//Do something
}
}
private void callExternalAPI() throws ClassNotFoundException {
thirdPartyLibrary.finalInvokeMethod();
}
I have a lot of custom exceptions that I'm throwing in a specific cases in the code, and I'd like to have one catch block at the bottom of the method to handle them all.
All the exceptions are children of the Exception class CribbageException, so I'd like to have:
public void myMethod(){
if (whatever){
throw new CardException();
}
if (something else){
throw new InvalidCardException();
}
if (scenario 3){
throw new TwoCardsException();
}
catch (CribbageException e) {
System.out.println(e.getMessage());
}
}
But I'm getting a catch without try error.
Is there any way to use this type of exception handling?
Wrap all the throws inside a single try.
public void myMethod(){
try {
if (whatever){
throw new CardException();
}
if (something else){
throw new InvalidCardException();
}
if (scenario 3){
throw new TwoCardsException();
}
}
catch (CribbageException e) {
System.out.println(e.getMessage());
}
}