Standard coding style for try catch

?

-

What's the standard coding for try and catch?

e.g

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
...
}
 
W

Wibble

- said:
What's the standard coding for try and catch?

e.g

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
...
}
Theres no standard. Dont ignore exceptions though.
 
J

John C. Bollinger

- said:
What's the standard coding for try and catch?

e.g

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
...
}

This is not a style question, but rather a question of
exception-handling strategy. Much depends on the nature and role of
AAA, on the contract of method(), and on the details of the "long coding
here". The best approach might be none of the above: declare method()
to throw that type of exception, and don't catch it within method() at
all. As Wibble already told you, however, the specific choice of
silently ignoring exceptions is generally considered unwise.
 
C

Chris Smith

- 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
 
A

Anton Spaans

- said:
What's the standard coding for try and catch?

e.g

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
...
}

No specific style is better. But if AAA is a resource that needs to be
cleaned up, Style 3 is your option:

Style 3:
public void method() {
AAA a = null;
try {
a = new AAA();
// some long coding here, where exceptions could be thrown.
....
} catch (...) {
return;
}
finally {
if (a != null) a.release();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top