If the same exception can be thrown from multiple places, does it make sense to create one instance and throw it from both?

John Zukowski

When you create the Throwable/Exception instance, stack trace information is placed into the object. This is used to report the origin of the exception for the stack dump. If you create the exception at a place other than where the exception actually occured, the stack dump will actually be incorrect. It is best to combine the creation of the exception object with the use of the throw keyword to make sure the information is reported correctly:

throw new IOException("Oops, sorry about that.");

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact