In that simple example, there's no need for try..finally
; g2D.drawLine
does not throw exceptions.1 However, if the body of the try
might throw an exception, execute a return
statement, or otherwise abnormally terminate the paint
method, then I would recommend try..finally
to ensure that the context is properly disposed.
1 It could, I suppose, throw an OutOfMemoryError
or some other unchecked exception. If it does that, though, disposing of a Graphics2D
context will be the least of your problems.