File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to throw exception from class level static block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to throw exception from class level static block?" Watch "how to throw exception from class level static block?" New topic
Author

how to throw exception from class level static block?

Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
hi all,
i have a query...
suppose i have a class and there is a class level static block. in that i want to handel some exceptions. so i write a try-catch block..
now the problem is that... i want to throw a new customised exception based on that exception... now when i write throw clause in catch block of that static block then comiler gives an error saying
Unhandeled Exception
can anyone tell me how to handel this??
Thanks a lot in advance for your time and efforts..
Amit


****************************<br />In 24 hrs Earth rotates once on its Axis.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Posting some code would help us identify the problem.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I don't think that a static block is allowed to throw a (non-Runtime) exception.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
the code is following:-

Test.java:9: unreported Exception java.lang.Exception; must be caught or declared to be thrown
can any one help now??
Thanks in advance
Amit
Tamizh Selvan
Greenhorn

Joined: Oct 10, 2003
Posts: 15
hi
whenever an execption is thown it must be caught. in your case u r throwing an exception in the catch block hence, even that must be caught. to do this use nested try catch block. and it has nothing to do with static and all the stuff.
public class Test {
static int i=0;
static int j=0;
static {
try{
i=i/j;
}catch(Exception e) {
System.out.println("***** Error in Static Block******");
try{
throw e;
}catch(Exception e1){
System.out.println("");
}
}
}//end of static block

void foo(){
System.out.println("in foo()");
}
public static void main(String[] argv) {System.out.println("Starting.....");
}
}
Hope this help u.
Marcus


"No one knows what one can do until one tries"<br />-John Kingsley.
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
hi Marcus,
actually i am using this sort of code for my web application using.. EJB.. in which one servlet calls another servlet which has static block...
now if somehting goes wrong in that static block then the caller servlet should know about it.. thus i throw exception in catch block of the static block...
assume this to b a class .. assume i am calling a merhtod os this class from some other class.. i want the caller class to know what went wrong...
so can anyone give me idea on how to achieve this...
Thanks in Advance
Amit
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The problem is that the static block will be executed when the class is loaded - which is not known at compile time. But it is the compilers responsibility to assure all checked exceptions are handled somewhere. It can't do this for exceptions thrown by a static block, so it is simly not allowed.
Try converting the checked into an unchecked Exception (for example by "throw new RuntimeException(e);").
BTW, what exactly are you doing in the static block? I find that I don't use them very often...
Neil Mac
Greenhorn

Joined: Oct 03, 2003
Posts: 7
Static blocks can't pass on any checked exceptions (i.e. throw them) - it must catch them or throw an unchecked exception.
An instance block can throw checked exceptions but they MUST be handled in EVERY constructor. But an instance block is probably of no use to you.
So, short answer..... you can't
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"Spider Man" - please take a moment to read our display name policy and then edit our profile to use a name which is not obviously fictitious. Thanks.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Try converting the checked into an unchecked Exception (for example by "throw new RuntimeException(e);").
Looking at Amit's code, the exception in questino already is a RuntimeException - it's actually an ArithmethicException for dividing by zero. But since it was caught by a catch Exception, the compiler figures that the thing you're throwning now could be any exception, which means it's now treated as a checked Exception rather than an unchecked RuntimeException. So, to get this to compile all you need to do is:

However, this is still a problem. If you run this, you will see that what actually gets trhown is an ExceptionInInitializerError. Error, not Exception. That's because any time any exception is thorwn from a static initializer, the class is considered to be invalid (the initializer is run as part of the class loadig process) and there's really no sensible way to recover - the system simply cannot trust the class that's been loaded, and there's no easy way to reload it. So the JVM protests in the strongest terms possible, to force you to fix the problem.
So - you really shouldn't be throwing any exception from a static initializer unless your intent is to stop the progeam entirely. Which is often entirely appropriate. Just realize that you're stopping the program; no recovery will be possible.
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
Thanx a lot Jim,
i think i will have to figure out some other way to do this...
as it is part of EJB and and i really need to find out what went wrong in static block if something really goes wrong..
Thanx a lot guys for u alls support
Amit
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Amit KumarS:
as it is part of EJB and and i really need to find out what went wrong in static block if something really goes wrong..

But could the application automatically recover from the problem?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to throw exception from class level static block?