- said:
What's the standard coding for try and catch?
It depends on the goal. If the goal of the catch block is to catch and
semantically handle a specific exception from a specific operation, then
the block should be as small as it can while still handling that
exception; and that favors your second "style". If the goal is to act
as a kind of "catch-all", for example, to transform exceptions into a
more appropriate abstraction and rethrow, then wrapping an entire method
in a catch block is a fine way to proceed.
Note that this isn't really "style", since it can actually affect the
behavior of the application. Moving code into the end of a try block
(from the section right after the try/catch) means that even if an
exception is handled and the catch block completes normally, that code
will be skipped.
Style 1:
public void method() {
try {
AAA a = new AAA();
...
// some long coding here
...
} catch (...) {
}
}
Style 2:
public void method() {
AAA a;
try {
a = new AAA();
} catch (...) {
return;
}
...
// some long coding here
...
}
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation