Dashboard > WG: Programmer's Cafe > ... > Eclipse Tips > Adding a try-catch block
  WG: Programmer's Cafe Log In | Signup View a printable version of the current page.  
  Adding a try-catch block
Added by Aaron Zeckoski, last edited by Aaron Zeckoski on May 21, 2006
Labels: 
(None)

  • This shows how to use Eclipse to insert a try-catch block around a statement which automatically includes possible exceptions
  1. Select the statement to surround with a try-catch block
  2. Right click on the selection and choose Source -> Surround with Try / Catch Block
  3. You should now edit the Auto-generated catch block(s)
    • Note: don't forget to remove the e.printStackTrace(); if you do not want this exception to put a stackTrace in the logs

Best practice: propagate all exceptions that you don't know how to deal with as some kind of RuntimeException. You may choose to log them at source as well but this tends to make for somewhat noisy logs with the same exception occuring several times.

Either use JDK 1.4+ "cause" system to wrap the original exception (you will need to select an existing exception type that will fill in "cause" or derive one yourself), or, even better, use the PonderUtilCore "UniversalRuntimeException" (delivered as part of RSF!) which guarantees never to do more than one level of wrapping.

See Bruce Eckel at http://www.mindview.net/Etc/Discussions/CheckedExceptions
See URE at
http://www2.caret.cam.ac.uk/rsfwiki/Wiki.jsp?page=UniversalRuntimeException

Site running on a free Atlassian Confluence Open Source Project License granted to Sakai Foundation. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators