• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, there
do you know how to construct code to catch the exception thrown at line 1?
class SomeThrowable extends Throwable { }
class MyThrowable extends SomeThrowable { }
class foster extends MyThrowable{}
public class TestException
{
public static void main(String args[]) throws SomeThrowable
{
try{
m1();
}catch(SomeThrowable e){
}
finally{
System.out.println("Done");
throw new foster();//line 1
}
}
public static void m1() throws MyThrowable
{
throw new MyThrowable();
}
}
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try-catch block inside the finally block should do it

Necessary when handling IO streams. Commonly you can close the streams inside the finally block, but the close method also throws IOException, so you have to put the close invocation inside a try-catch block inside the surrounding finally block.

HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[This message has been edited by Valentin Crettaz (edited November 25, 2001).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually quite a common thing to do. Take JDBC code, for instance. Good JDBC code (unfortunately, this should not be confused with "the majority of JDBC code") closes its Statements and/or database connections in a finally { } block so as to avoid resource leaks.But, if there was a JDBC exception thrown inside the try { } block, it is well possible that the close() statement would throw an exception as well. This exception would obscure the first one, which is a pity because the first exception is likely to be more helpful in pinpointing the problem.This code will prevent the close() statement from obscuring any previously thrown exception. It is quite unfortunate that this is so cumbersome to do, and I must say that code like this is pretty rare.
- Peter
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic