aspose file tools*
The moose likes Beginning Java and the fly likes static block and exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static block and exception handling" Watch "static block and exception handling" New topic
Author

static block and exception handling

P. Ingle
Ranch Hand

Joined: Apr 25, 2005
Posts: 45
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 ]

Thanks,<br />P.Ingle
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: May 20, 2005
Posts: 61
How would you be able to handle an exception that shuts down the JVM anyways?


-Rich, SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Peter Korsten
Greenhorn

Joined: Feb 18, 2009
Posts: 2
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

Joined: Mar 20, 2007
Posts: 317
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

Joined: Feb 18, 2009
Posts: 2
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: static block and exception handling