File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes ex.getNextException(); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "ex.getNextException();" Watch "ex.getNextException();" New topic


MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188

In above code, how does the exceptions get chained to catch block. Once an exception is thrown the control is transferred to matching catch block. What I think is there can be only one exception at a time in a catch block, so I was wondering with the use of ex.getNextException() method.

Correct me if I am wrong in getting the concept.

I am slow but sure
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33117

Exception chaining is for different levels of information about the same issue rather than totally different exceptions.

See Sun's page that explains this well. The beginning is particularly informative:

It is common for Java code to catch one exception and throw another:

try {
} catch(YourException e) {
throw new MyException();

Unfortunately, the information contained in the "causative exception" (YourException in the example above) is generally lost, which greatly complicates debugging. Recognizing this problem, developers sometimes build ad hoc mechanisms to allow certain "wrapped exceptions" to contain a second exception. An accessor is generally provided to extract the contained exception. Such mechanisms are sometimes known as "exception chaining facilities", as they allow arbitrary chains of exceptions to be constructed when contained exceptions are, themselves, wrapped exceptions.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: ex.getNextException();
It's not a secret anymore!