• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling-- Praxis17 (from Practical Java)doubt.  RSS feed

 
manjusha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
In Praxis 18 of Practical Java book ( Never hide an exception), it says that if more than one exception is thrown in the catch or finally block then only the last exception is propogated , there by hiding all the exceptions. I understood that part. But I have a doubt in a slightly modified scenario.
The first scenario is explained in the book in Praxis 18.
The second scenario (my question) is :

try
{
//Execute a sql query.
}
catch (SQLException e)
{
System.out.println("SQLException occured: " e);
throw new Exception("Could not execute SQL query"); // Line1
}
finally
{ // Release the resources
try
{
if (ps != null) { ps.close(); ps = null; }
}
catch(SQLException e)
{
System.out.println("SQLException while closing statement object");
}
try{
if (dbconn != null) { dbconn.closeConn(); dbconn = null; }
}
catch(SQLException e)
{
System.out.println("SQLException while releasing connection");
}
}

My question is :
If an exception occurs in try block and also in the finally block, will it still throw the new exception at Line1.
I feel the anwer is yes because I am catching all other exceptions.
It would be great if someone can confirm.
Thanks,
Manjusha

[This message has been edited by manjusha (edited June 13, 2001).]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"manjusha",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
From what I understand from the book, and my experiences, is that ONLY the last 'thrown' exception is actually thrown or propogated to the calling method.
This isn't the same as 'catching' an exception.
Looking at it another way, omit catching anything and just declare your method to throw the diffrent exceptions. Only the last one thrown will be returned, not every possible caught exception.

------------------
SOURCE CODE should be SURROUNDED by "code" tags.
Click here for an example
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If no exception occurs in the finally, the exception from line 1 will propagate. If however, an exception were generated in the finally, it would be propagated, thus hiding the exception thrown from line 1. The bottom line is the last exception thrown from a method is the only one propagated. The rest of hidden. This is usually bad since the first exception thrown is the reason why the method failed, and thus the most important one to return.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!