When an exception occurs during the handling of an exception, only the last exception gets reported, because I can add just one exception to an Error
object. How to report all exception in the final error message?
Example:
class main
{
public static void main (String[] args)
{
try {
// Database.insert ();
throw new Exception ("insert failed");
}
catch (Exception ex1) {
try {
// Database.rollback ();
throw new Exception ("rollback failed");
}
catch (Exception ex2) {
throw new Error ("Can not roll back transaction.", ex2);
}
}
finally {
try {
// Database.close ();
throw new Exception ("close failed");
}
catch (Exception ex3) {
throw new Error ("Can not close database.", ex3);
}
}
}
}
In the example everything fails. The database insert causes ex1
. The rollback causes ex2
. And closing the database causes ex3
. When the program gets executed only the last ex3
gets reported in the Error
object. How to include also ex1
and ex2
to the Error
object? The constructor of Error
accepts just one exception.