开发者

what exceptions this code may throw ?

I want to know which kind of exc开发者_运维知识库eptions can be thrown by this code so i can catch them instead of just catching the generic exception (trying to reproduce errors to cause exception is difficult here cause it takes a lot of time to set up the request used )

JAXBContext jc = JAXBContext.newInstance(QueryReport.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));

marshaller.marshal(requestService, out);
is = new ByteArrayInputStream(out.toByteArray());
JAXBReader jcReader = new JAXBReader("QueryReport");

log.debug("\n# XML QueryRequest Response: " + jcReader.read(is).asXML());

so if anyone has an idea of which exceptions maybe thrown here.

Thanks


Eclipse, Netbeans or any other modern IDE will tell you precisely which exceptions are raised.

I'm guessing you'll see at least ClassNotFoundException, IOException and JAXBExceptions.


From the code block you've shown, the possible exeception will be JAXBException from calling JAXBContext and ClassNotFoundException if QueryReport.class cannot be located from the classpath and IOException if call to ByteArrayInputStream failed.

You can use your IDE to wrap the relevant portion of code with generated try/catch block, with the Exception it see fit for the syntax block.


Assuming you are talking about unchecked exceptions, there's generally a good reason the API designers decided to not make them checked exceptions. But if you must absolutely catch them, then you should read the API for the methods you are using.


Why do you need to catch them separately? What are you going to do differently for each one? If the answer is "nothing", then just catch them generically.

If you haven't already, read the sections on exceptions in Effective Java, and/or read "Effective Java Exceptions" (different author).

If I'm preaching to the choir - my apologies.

At some level near the top of your program you'll probably want to catch unchecked exceptions - things like NullPointerException.


If you explicitly need to catch any exceptions then these will be checked. This means you only need to catch exceptions that the compiler tells you.


Are you using an IDE? Eclipse (via the compiler) will tell you exactly what exceptions will be thrown, and even generate the boilerplate catch blocks for those exceptions. Or, you can look at the API for each of the method calls to see what the possible exceptions are.


If you don't enclose this code block into any try block, the compiler will tell you which exceptions must be caught or thrown by the method.

Any other exception that might be thrown by this code will be a runtime exception, which is a sign of a programming error, and which should thus not be caught, because it would just hide a bug rather than force you to fix it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜