Recursive Reflection and Exceptionhandling

I'm processing an XML-file including the Element <Content> and others. To process i use reflection so my methods are recursive automaticaly called. The disadvantage is that every thrown exception ends in a InvocationTargetException in the root. So the excetions are not very usefull. Can i get the real thrown exception via reflection. Is there any design trick I can use?

publicvoid addContent(Node node, Object obj, Class theClass)throws Exception

{

String value= ((Element)node).getAttribute("value");

if(value.equals(""))

{

thrownew MyException("Value-Attribute is not optional", node);

}else{

System.out.println(value);

}

processChildElements(node, paramObj, paramClass);

}// addContent

privatevoid processChildElements(Node node, Object obj, Class cl)throws Exception

{

if(((Element)node).getElementsByTagName("*") !=null)

{

Node childNode =null;

//process child elements

NodeList nlChild = ((Element)node).getChildNodes();

for(int i=0; i<nlChild.getLength(); i++)

{

childNode = nlChild.item(i);

if(childNode.getNodeType() == Node.ELEMENT_NODE){

String nodeName = childNode.getNodeName().trim();

Method method = this.getClass().getMethod("add" + nodeName,new Class[]{

org.w3c.dom.Node.class,

java.lang.Object.class,

java.lang.Class.class});

method.invoke((Object)this,new Object[]{childNode, obj, cl});

}

}

}

}

>

[2852 byte] By [maglight_ch] at [2007-9-26 2:07:38]
# 1
try putting a try catch block around the code and catching the InvocationTargetException. Then throw the real exception by usingthrow ite.getTargetException()where it is the InvocationTargetException
scottcoleman1973 at 2007-6-29 8:55:09 > top of Java-index,Archived Forums,Java Programming...