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.
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.