Computer Science Canada

try-catch-finally

Author:  Martin [ Thu Oct 13, 2005 9:42 pm ]
Post subject:  try-catch-finally

Quick question.

What's the difference between #1 and #2:

code:
///#1
try  {
        foo();
}
catch (SomeException e) {
        bar();
}
finally {
        baz();
}


///#2
try  {
        foo();
}
catch (SomeException e) {
        bar();
}
baz();


Thanks in advance.

Author:  wtd [ Thu Oct 13, 2005 9:52 pm ]
Post subject: 

What happens if an exception other than SomeException is thrown?

The first example will run baz. The latter won't.

Author:  Martin [ Thu Oct 13, 2005 10:18 pm ]
Post subject: 

Ahh, got it. Thanks.

Author:  Finaltank [ Fri Oct 14, 2005 3:26 pm ]
Post subject: 

What about interrupted exception?
code:

try
{
Thread.sleep (Time in parenthesis)
}
catch (InterruptedException e)
{
}

Author:  Hikaru79 [ Fri Oct 14, 2005 4:27 pm ]
Post subject: 

InterruptedException is just a subclass of Exception. You can catch exceptions specifically (like in your example) so that you can deal with different exceptions in different ways, or you can catch a generic "Exception" which handles any subclasses of Exception (most of them).


: