How to create objects of Exception using reflection?

Hi all,

How to create objects of Exception using reflection?

How to create object of specified Exception using its name?

Because Exception is not the child of Class, the Class.forName(...) is useless.

The second question is how to use reflect and Proxy on abstract Class?

I found them can only be used on Interface.

Regards,

Jerry

[379 byte] By [Jarod_Hantona] at [2007-9-25]
# 1

WHAT?

Exceptions are just like every other class - you create instances in exactly the same way, either using the 'new' operator, or reflection, or Class.newInstance(), or whatever.

The statement "because Exception is not the child of Class, the Class.forName(...) is useless" is completely meaningless. There are no "children" (by which I assume you mean sub-classes) of Class - it is final. By your argument this means that no object instances could be created this way, which is clearly not true.

dannyyatesa at 2007-7-15 > top of java,Core,Core APIs...
# 2

Hello,

I found that this problem only exists in old version Java(Java 1.4), for Java 1.5, it not exist.

--

import org.xml.sax.SAXException;

public class ExceptionTest {

public static void main(String[] args) {

SAXException sa = null;

try{

sa = (SAXException)Class.forName("org.xml.sax.SAXException").newInstance();

}catch(Exception e){

e.printStackTrace();

}

}

}

java.lang.InstantiationException: org.xml.sax.SAXException

at java.lang.Class.newInstance0(Class.java:293)

at java.lang.Class.newInstance(Class.java:261)

at ExceptionTest.main(ExceptionTest.java:6)

-

Is there any one can tell how to use reflection to create Exception object in Java 1.4.

Thanks

Jarod_Hantona at 2007-7-15 > top of java,Core,Core APIs...
# 3

OK...

1) Why the hell are you trying to do this?! Just do "new SAXException..."

2) Read the documentation of Class.newInstance()

2a) the bit about what newInstance() is equivalent to

2b) the bit about what exceptions it can throw and why

3) Observe that SAXException in Java 1.4.2 does not meet the criteria laid out in the documentation of newInstance()

4) Observe that SAXException in Java 1.5.0 DOES meet those criteria

dannyyatesa at 2007-7-15 > top of java,Core,Core APIs...
# 4

OK...

1) Why the hell are you trying to do this?! Just do "new SAXException..."

-

I need to write a set of negative JUnit test cases to test the exception handling in my code.

So using "new" just force me to write a lot of useless code.

If I can use reflection and loop to generate Exception objects, of course, it is better.

--

2) Read the documentation of Class.newInstance()

-

Of course, I have read the Java documnt. You can see that it throws InstantiationException.

InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.

This part of explanation can not explain the exception I met. SAXException is not an abstract class, an interface, an array class, a primitive type, or void; does not have nullary constructor. Maybe it is caused by "some other reason".

-

2a) the bit about what newInstance() is equivalent to

-

I don't know. Maybe you know the better way to do the same work.

2b) the bit about what exceptions it can throw and why

--

JUnit test

-

3) Observe that SAXException in Java 1.4.2 does not meet the criteria laid out in the documentation of newInstance()

4) Observe that SAXException in Java 1.5.0 DOES meet those criteria

-

SAXException is just a example. Actually I found in Java 1.4, this happens to all Exception(I have not tested them all. But I found at least the Exceptions I used all in this way). For the project requirement, I have to use Java 1.4.

And I don' t know what is the criteria laid out in the documentation of newInstance(). If you know you can tell me

--

I don't need to use Class.newInstance(). If you know some way to get around it, please tell me.

Thanks

Jarod_Hantona at 2007-7-15 > top of java,Core,Core APIs...
# 5

*sigh*

This is the last piece of spoon feeding you will get from me...

You have already identified the problem with what you said about "nullary constructors". Class.newInstance() throws that exception "if the class has no nullary constructor", and as you yourself have said "SAXException ... does not have a nullary constructor".

The documentation also says "This class is instantiated as if by a 'new' expression with an empty argument list"... another way of saying the nullary constructor thing.

In Java 1.5 SAXException has had a nullary exception added.

This WILL work if you pick classes that meet the criteria listed. Period. If it's not, you've got some other problem with your code. I just picked two exceptions from 1.4 at random: ArrayIndexOutOfBoundsException and NullPointerException. Both of these have nullary (or no-args) constructors, and will work fine. Sounds like you have been unlucky picking the Exceptions you are trying.

I still don't understand why you aren't creating these using the 'new' operator, but I don't really care any more either...

dannyyatesa at 2007-7-15 > top of java,Core,Core APIs...
# 6
Thanks.Now I understand the reason.Thank you for the explanation of nullary constructor.
Jarod_Hantona at 2007-7-15 > top of java,Core,Core APIs...
# 7

the JDK does not provide a way to proxy classes. if you really want to do this, use a library such as CGLIB, but if you're coding to an interface ( you are, aren't you? ) rather than a class, this should not be necessary. obviously if you're wanting to proxy a third-party class, CGLIB is the way to go. beware, though, the library is a bit leaky!

georgemca at 2007-7-15 > top of java,Core,Core APIs...