This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to throw exception from class level static block?

 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting some code would help us identify the problem.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that a static block is allowed to throw a (non-Runtime) exception.
 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit KumarS
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic