• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block and exception handling

 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can I throw exception from a static block???

class MyClass throws Exception{
static{
try{
// do some thing like loading some library
System.load("MyLibrary");

} catch (Exception e){
throw new Exception("Failed to load MyLibrary");
}
}//end static

MyClass(){ // constructor }

} // end MyClass

But then I would have to add 'throws Exception' clause after class declaration as shown above - which I don't think is right ( and it also gives me error.)

I am loading native library in static block - which, if fails, shuts JVM all together. I would like to have some graceful way of handling this situation.

How can I do this?

Thanks,
P.Ingle
[ July 08, 2005: Message edited by: P. Ingle ]
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by P. Ingle:
I am loading native library in static block - which, if fails, shuts JVM all together. I would like to have some graceful way of handling this situation.


if the failure really should exit the entire program, then you don't need to throw an exception in order to achieve this — you can just print an error message and System.exit() to get out. in fact, that would likely be more graceful than leaving the user with a stack dump from an unhandled exception on the screen.
 
Richard Anderson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you be able to handle an exception that shuts down the JVM anyways?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IIRC, you can put the throws clause after the static keyword:

Also note that it is usually considered a good practice to throw (or catch) specific Exception subclasses rather than just throwing (or catching) Exception itself. This is primarily because when you catch Exception, this also includes Runtime errors that usually indicate a programming error such as NullPointerException.

HTH

Layne
 
Peter Korsten
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't put a throws clause after the static keyword.

What you need to do is throw an ExceptionInInitializerError. From the JavaDocs:

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.


Which is exactly what you're looking for.

- Peter
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Peter,

Welcome to Javaranch.
You might also want to take a look at This.

Its a 3.5 year old thread
 
Peter Korsten
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it's a thread that contains a couple of incorrect answers, and yet shows up as one of the first hits on Google, which likely would lead other developers down the wrong path.

Apart from that, throwing an exception from a static block that will not exit your application is probably a bad idea.

- Peter
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Korsten wrote:What you need to do is throw an ExceptionInInitializerError.

Well, we don't quite need to, in the sense that any other exception we throw will be caught by the JVM, wrapped in an ExceptionInInitializerError, and then rethrown. So if we want to throw some other (unchecked) exception with more specific information about what went wrong, that works too. There are pros and cons to each approach, but they both end up with the result that the class is subsequently unusable, and letting the JVM exit is often the best strategy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic