Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with nested try-catch...

Status
Not open for further replies.

Mol

Programmer
May 1, 2001
10
SE
This is probably an easy one :)

I´ve got some problems with a few nested try-catch statements. The problem is that the second catch-statement is never reached, even if an exception occurs when I´m trying to use an invalid resource-key.

I need to use the ResourceBundle to form a user-friendly string...

Here is the code:

try
{
// Always throws an exception
resultSet = statement.executeQuery();
}
catch(SQLException e)
{
// Prints the exception allright...
System.out.println(e);

try
{
System.out.println(oSQLResourceBundle.getString(Integer.toString(e.getErrorCode())));
}
catch(MissingResourceException e1)
{
// Does not reach this row
System.out.println(e1);
}

System.out.println(e);
}

Is there anyone out there that´s got an idea about this?
Really appreciate it.

Mol
 
I tried a similar simulation myself and it worked for me. It's possible that you've narrowed the Exception to hard or ask for an other exception than that is thrown.
Try to include all the exceptions that are thrown in the second try/catch block (for instance, the NullPointerException can also be thrown), and include a general Exception catcher which isn't allowed to be handled.
Like:
try
{
System.out.println(oSQLResourceBundle.getString(Integer.toString(e.getErrorCode())));
}
catch(MissingResourceException e1)
{
// Does not reach this row
System.out.println(e1);
}
catch(NullPointerException e1)
{
// Perhaps this row then
System.out.println(e1);
}
catch(Exception e1)
{
// And if all fail, this will catch it
System.out.println(e1);
}

Hope this helps,
Steven.
 
Thanks for your time Steven.

I haven´t got a clue to why, but the exception is now thrown using the original code. Maybe it was a problem with the ResourceBundle or something.

Strange... (needless to say)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top