try {
throw new SomeException();
}
catch (SomeException e) {
System.out.println("reached once");
throw e;
}
catch (Exception e) {
System.out.println("reached twice");
}
This code only displays "reached once" even though the exception was thrown again inside the first catch clause. How can this be fixed in order that both catch clauses be executed?
PS: The above code was a general question I had, and I had to apply it to a much larger code with about 5 or 6 catch clauses that catch different exceptions, but in the end, at a certain point in a loop I need the exception to be thrown again.
Simply add another try catch in the catch.
try {
try {
throw new NullPointerException();
} catch (NullPointerException e) {
System.out.println("reached once");
throw e;
}
} catch (SomeOtherException ex) {}
You'll have to sorround all code that can throw an Exception with a try/catch block
try {
throw new NullPointerException();
}
catch (NullPointerException e) {
System.out.println("reached once");
try{
throw e;
}
catch (Exception ex) {
System.out.println("reached twice");
}
}
Related
I am able to catch RuntimeException or subclass of it with below code:
try {
//code that throws subclass of RuntimeException
throw new ChildRuntimeException("try");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
But I am getting error with below code and not able to catch RuntimeException in Exception catch block.
try {
// code that throws subclass of Exception
throw new ChildExceptionClass("try");
} catch (ChildExceptionClass ex) {
throw new RuntimeException(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
I searched for the same type of questions but did not find a suitable answer. Can
anyone explain why the behaviour is different?
In the second example you are throwing a childRuntimeException, which is caught, but then a new runtimeException is thrown. This block has no "catch" clause, so the exception is thrown and not caught.
The second catch is relevant for the "try" block, not for the "catch" block.
What I guess you probably want to do is:
try { // code that throws subclass of Exception
throw new ChildExceptionClass("try");
} catch (ChildExceptionClass ex) {
try {
throw new RuntimeException(ex.getMessage());
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
Do you understand the difference?
I want to execute my method callmethod if the condition inside the IF statement is met. Else it should execute the catch block. But during implementation, if the condition is not met, it does not go to the catch block.
try{
if(count==0)
callmethod();
}
catch (Exception e){
System.out.println(e);
}
This is a good application for methods:
try {
if (count == 0) {
callOneMethod();
}
else {
callOtherMethod();
}
catch (Exception e) {
callOtherMethod();
}
That way you don't have any duplicated code and you're not doing weird things with exceptions in non-exceptional cases.
Since you are trying to hit the catch block, you need to throw an exception if your parameter is not met (i.e. count != 0).
Example:
try {
if(count==0){
callmethod();
} else {
throw new SomeException("some message");
}
}
catch (Exception e){
System.out.println(e);
}
I have the following:
public void method(){
try {
methodThrowingIllegalArgumentException();
return;
} catch (IllegalArgumentException e) {
anotherMethodThrowingIllegalArgumentException();
return;
} catch (IllegalArgumentException eee){ //1
//do some
return;
} catch (SomeAnotherException ee) {
return;
}
}
Java does not allow us to catch the exception twice, so we got compile-rime error at //1. But I need to do exactly what I try to do:
try the methodThrowingIllegalArgumentException() method first and if it fails with IAE, try anotherMethodThrowingIllegalArgumentException();, if it fails with IAE too, do some and return. If it fails with SomeAnotherException just return.
How can I do that?
If the anotherMethodThrowingIllegalArgumentException() call inside the catch block may throw an exception it should be caught there, not as part of the "top level" try statement:
public void method(){
try{
methodThrowingIllegalArgumentException();
return;
catch (IllegalArgumentException e) {
try {
anotherMethodThrowingIllegalArgumentException();
return;
} catch(IllegalArgumentException eee){
//do some
return;
}
} catch (SomeAnotherException ee){
return;
}
}
here is a code:
try {
FileOutputStream fout=new FileOutputStream("path");
javaClassFun(url,fout);
fout.close();
} catch (MalformedURLException ex) {
System.err.println("Invalid URL"+ex);
} catch (IOException e) {
System.err.println("Input/Output error"+e);
}
when i cut the last catch block and paste it after try block it gives unreachable catch block error.
I want to know what is the reason behind this.
The reason why is that MalformedURLException inherits from IOException.
try {
//call some methods that throw IOException's
} catch (IOException e) {
// This will catch MalformedURLException since it is an IOException
} catch (MalformedURLExceptionn ex) {
// Will now never be caught! Ah!
}
If you want to design catch blocks which properly handle an exception hierarchy, you need to put the super class last and the subclasses which you want to handle individually prior to it. See the example below for how to handle the IOException class hierarchy as it pertains to your code.
try {
//call some methods that throw IOException's
} catch (MalformedURLExceptionn ex) {
// This will catch MalformedURLException
} catch (IOException e) {
// This will catch IOException and all other subclasses besides MalformedURLException
}
I have a problem understanding how the try{} catch(Exception e){...} works!
Let's say I have the following:
try
{
while(true)
{
coord = (Coordinate)is.readObject();//reading data from an input stream
}
}
catch(Exception e)
{
try{
is.close();
socket.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
Section 2
try
{
is.close();
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Let's say my while() loop throws an error because of an exception of is stream.
This will get me out of the infinite loop and throw me in the first catch(){............}
block.
My question is the following:
After throwing an exception, getting out of the loop while() and reaching to
catch(){
}
Will my program continue his execution and move on to section 2? As long as the exception was caught? Or everything ends in the first catch()?
I think you want to use finally after your first catch [catch (Exception e)] to close your streams:
try {
// Do foo with is and db
} catch (Exception e) {
// Do bar for exception handling
} finally {
try {
is.close();
db.close();
} catch (Exception e2) {
// gah!
}
}
As long as no exceptions are thrown in your catch clause, your program will continue execution after your catch (or finally) clause. If you need to rethrow the exception from the catch clause, use throw; or throw new Exception(ex). Do not use throw ex, as this will alter the stack trace property of your exception.
After the exception is caught, execution continues after the try/catch block. In this case, that's your section 2.
An uncaught exception will terminate the thread, which might terminate the process.
Yes, you are right. It will move to Section 2.
If you want your Section 2 bound to happen, irrespective of any exception generated, you might want to enclose them in a finally block.
try {
// Do foo with is and db
}
catch (Exception e) {
// Do bar for exception handling
}
// Bound to execute irrespective of exception generated or not ....
finally {
try {
is.close();
db.close();
}
catch (Exception e2) {
// Exception description ....
}
}