How To Handle Exceptions From Static Code Block in Java
By Angsuman Chakraborty, Gaea News NetworkThursday, July 6, 2006
Exception can be handled when thrown in a method by either passing through the Exception or handling it. However in a static code block how can you handle checked exceptions meaningfully?
Normally static blocks are used to execute a code only once globally (for all instances of the class). A popular example is loading the JDBC driver. Sometimes the code in static block throws checked Exceptions which needs to be handled. Simple try-catch block doesn’t do justice to the problem as normally failure in static code block means the program cannot continue.
A clean way to handle it is using a try-catch block. However after logging appropriate error messages you should throw a RuntimeException. The normally ends the program execution.
Update: After logging the exception you have two options. You can either throw a RuntimeException which will end the current thread (unless caught by code instantiating / calling a static method on the class for the first time) or better yet you can call System.exit(1). RuntimeException will end a single threaded application[Laszlo] (exception noted above). In JDK 1.5 throwing RuntimeException from main thread gives a pretty descriptive message:
Caused by: java.lang.RuntimeException: Error!
at TestStaticException.
At this point it may be argued that System.exit(1) is not desirable in a managed environment like a servlet and I agree. System.exit is for java applications and only if the static initializer block performs some critical (without which the program cannot be run successfully) function like loading the database driver. RuntimeException may be consumed in a managed environment. So a third option is to set a flag indicating failure. Later the constructors can check the flag and throw exceptions [Robert] or retry in rare cases.
If the operation is not important to the functioning of the program (like setting fonts or laf) then maybe a simple log entry is all that is required.
February 23, 2010: 3:11 am
public static void exit(int status) Terminates the currently running Java Virtual Machine. |
Appalled |
January 10, 2010: 2:39 am
This is just horrible. Who are you? System.exit(1), are you serious? Have you ever written an actual working program professionally? I think you need to know something about what you’re talking about before trying to “teach” others. |
September 13, 2007: 3:21 am
sir i want the details of java . |
me |
me |
August 3, 2006: 5:16 pm
can you send me to my email some smples of a handle exception…..plzzzzzzzzzz app or more |
Ron |
July 11, 2006: 7:35 pm
Why not throw an ExceptionInInitializerError()? static { |
July 7, 2006: 12:26 am
The advice on how to handle exceptions in static blocks is actually quite dangerous. See https://twasink.net/blog/archives/2006/07/how_not_to_hand.html for more (This would have been a trackback, but trackbacks don’t seem to show up here) |
July 6, 2006: 5:46 pm
That would end the thread and not program execution. (It may result in program termination if you have only one thread.) I guess you know it, but it is more correct put this way :). |
NoName