How do I surround code with a try/catch block?
Author: Deron Eriksson
Description: This tutorial describes how to surround code with a try/catch block in Eclipse.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
One extremely common task that occurs when writing JavaSW applications is to catch exceptions via a try/catch block. EclipseSW can automatically generate a try/catch block for you. As an example, below we have an instantiation of a FileWriter object, which throws an IOException. ![]() Let's automatically generate a try/catch block to handle this exception. To do so, I highlight the code that I'd like to surround with the try/catch block. I then right-click and select Surround With → Try/catch Block. ![]() This automatically generates a try/catch block, as shown below. The IOException is automatically imported, and the catch block outputs the stack trace to standard error via e.printStackTrace(). ![]() This is one of the Eclipse tools that I consider essential. |