how do i get/handle exceptions in a static block?

static{

try{

Test a =new Test();

test.getResult();

}

catch (Exception e){

...

}

}

assuming getResult will result to a NullPointerException, how can i get the error msg like for example i want to log that error?

[563 byte] By [kneelmara] at [2007-11-27 8:12:58]
# 1
You could always use e.getMessage().
Kosmosisa at 2007-7-12 19:57:25 > top of Java-index,Java Essentials,Java Programming...
# 2
or e.printStackTrace();
floundera at 2007-7-12 19:57:25 > top of Java-index,Java Essentials,Java Programming...
# 3
i mean what if the one using that class where the static block is located will be the one logging the error?
kneelmara at 2007-7-12 19:57:25 > top of Java-index,Java Essentials,Java Programming...
# 4
Please explain what you mean in more detail please.
floundera at 2007-7-12 19:57:26 > top of Java-index,Java Essentials,Java Programming...
# 5
What?
paulcwa at 2007-7-12 19:57:26 > top of Java-index,Java Essentials,Java Programming...
# 6
Hang on. Are you saying that a different class is calling that code (I sure hope it is in a method)? Then what you need to do is throw the exception and have the try/catch block in the class that is calling it.
floundera at 2007-7-12 19:57:26 > top of Java-index,Java Essentials,Java Programming...
# 7

static blocks can't throw Exceptions so if you do encounter one the only thing you can do is handle it and set the class up in a way that indicates to methods that the static initialiser failed (and log things of course).

Those methods can then throw their own Exception to indicate that failure.

jwentinga at 2007-7-12 19:57:26 > top of Java-index,Java Essentials,Java Programming...