Catch the same exception twice - java

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;
}
}

Related

Java Try Catch Exception

My current block of codes returns an exception like:
Exception occurred in API invocation A1-123 Fatal error
Caused by: A9-001 ColName is not found in TableName
but I would like to...
get rid of A1 Exception
show A9 Exception directly
not show A9 Exception in Caused by
How do I make the exception look like this?
Exception occurred in API invocation A9-001 ColName is not found in TableName
<no Caused by clause>
This is my sample code:
public Sample Method (Input input) throws AException
{
con = getSQLConnection();
try{
//do something
if(x==null){
throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
}
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally{
if(con!=null){
try{
con.close();
}
catch(Exception e){
logger().error(e);
throw new AException(e);
}
}
}
}
Would it work if it's something like this:
try{
//do something
}
catch (Exception e){
logger().error(e);
throw new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
I'm assuming the error that you want to catch and pass on is an AException - otherwise what you're asking for would violate the contract of the method. You could achieve what you're wanting with an extra catch clause like this.
try {
// whatever
}
catch (AException ae) {
throw ae;
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally {
// whatever
}
That way, only exceptions that are not already of type AException will get wrapped in new AException objects.
the first commenter is correct. i was able to do it by creating a new first catch:
public Sample Method (Input input) throws AException
{
con = getSQLConnection();
try{
//do something
if(x==null){
AException ae = new AException(A9ErrorMessages.A9_ERROR_FROM_TABLE, new String [] { "ColName", "TableName"});
logger().error(ae);
throw ae;
}
}
catch (AException ae){
logger().error(ae);
throw ae;
}
catch (Exception e){
logger().error(e);
throw new AException(e);
}
finally{
if(con!=null){
try{
con.close();
}
catch(Exception e){
logger().error(e);
throw new AException(e);
}
}
}
}

IF inside try block

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);
}

Intellij Structural Search: How to find empty try catch blocks?

How can i find the empty try catch blocks?
Using the Copy existing template... I found the structural search for try catch:
try {
$TryStatement$;
} catch($ExceptionType$ $Exception$) {
$CatchStatement$;
}
I want to enhance it so that it does only find try catches with empty catch blocks
It should find:
try {
assertTrue(output.validate());
} catch (Exception e) {
//TODO something
}
or
try {
assertTrue(output.validate());
} catch (Exception e) {
}
or
try {
assertTrue(output.validate());
} catch (Exception e) {}
However not:
try {
assertTrue(output.validate());
} catch (Exception e) {
e.printStackTrace();
}
Right now it obviously finds both since there's no differentiation betweens.
How can I add this extra check?
Use the template you have found and on CatchStatement variable set Min count and Max count to 0.

How to catch an exception that was thrown inside a catch clause?

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");
}
}

Java Exception inside catch block

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
}

Categories

Resources