Exception propagation: When to catch exceptions?Handling an exception thrown in a catch blockUsing a try-finally (without catch) vs enum-state validationException hierarchy designShould we only catch in exceptional circumstances?Differences between `throw` and `throw new` and exactly how exceptions “bubble up”Handling exceptions I don't know aboutShould a C++ program catch all exceptions and prevent exceptions from bubbling up past main()?Catching Exception and Recalling same function?How should state machines handle exceptions in actions?Best practice for exception handling in Java threadsExceptions, error codes and discriminated unions
How are one-time password generators like Google Authenticator different from having two passwords?
Would encrypting a database protect against a compromised admin account?
Cropping a message using array splits
Passport stamps art, can it be done?
Is ‘despite that’ right?
My perfect evil overlord plan... or is it?
Why was wildfire not used during the Battle of Winterfell?
Watching the game, having a puzzle
How did Thanos not realise this had happened at the end of Endgame?
Is it bad writing or bad story telling if first person narrative contains more information than the narrator knows?
A Cunning Riley Riddle
The meaning of a て-form verb at the end of this sentence
Which other programming languages apart from Python and predecessor are out there using indentation to define code blocks?
Was there a contingency plan in place if Little Boy failed to detonate?
Ex-manager wants to stay in touch, I don't want to
Detect the first rising edge of 3 input signals
Thesis' "Future Work" section – is it acceptable to omit personal involvement in a mentioned project?
Has magnetic core memory been used beyond the Moon?
Why does it take longer to fly from London to Xi'an than to Beijing
Company threw a surprise party for the CEO, 3 weeks later management says we have to pay for it, do I have to?
Pre-1993 comic in which Wolverine's claws were turned to rubber?
Remove color cast in darktable?
Why use steam instead of just hot air?
Further factorisation of a difference of cubes?
Exception propagation: When to catch exceptions?
Handling an exception thrown in a catch blockUsing a try-finally (without catch) vs enum-state validationException hierarchy designShould we only catch in exceptional circumstances?Differences between `throw` and `throw new` and exactly how exceptions “bubble up”Handling exceptions I don't know aboutShould a C++ program catch all exceptions and prevent exceptions from bubbling up past main()?Catching Exception and Recalling same function?How should state machines handle exceptions in actions?Best practice for exception handling in Java threadsExceptions, error codes and discriminated unions
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
MethodA calls an MethodB which in turn calls MethodC.
There is NO exception handling in MethodB or MethodC. But there is exception handling in MethodA.
In MethodC an exception occurs.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
What is wrong with this ?
In my mind, at some point a caller will execute MethodB or MethodC, and when exceptions do occur in those methods, what will be gained from handling exceptions inside those methods, which essentially is just a try/catch/finally block instead of just let them bubble up to the callee ?
The statement or consensus around exception handling is to throw when execution cannot continue due to just that - an exception. I get that. But why not catch the exception further up the chain instead of having try/catch blocks all the way down.
I understand it when you need to free up resources, that's a different matter entirely.
design-patterns object-oriented-design exceptions
|
show 1 more comment
MethodA calls an MethodB which in turn calls MethodC.
There is NO exception handling in MethodB or MethodC. But there is exception handling in MethodA.
In MethodC an exception occurs.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
What is wrong with this ?
In my mind, at some point a caller will execute MethodB or MethodC, and when exceptions do occur in those methods, what will be gained from handling exceptions inside those methods, which essentially is just a try/catch/finally block instead of just let them bubble up to the callee ?
The statement or consensus around exception handling is to throw when execution cannot continue due to just that - an exception. I get that. But why not catch the exception further up the chain instead of having try/catch blocks all the way down.
I understand it when you need to free up resources, that's a different matter entirely.
design-patterns object-oriented-design exceptions
18
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
15 hours ago
With a good IDE and a proper coding style, you can know that some exception can be thrown when a method is called. Handle it or allow it to be propagated is the decision of the caller. I don't see any problem with this.
– Hieu Le
15 hours ago
3
If a method can't handle the exception, and is merely rethrowing it, I would say that is a code smell. If a method can't handle the exception, and doesn't need to do anything else when an exception is thrown then there is no need for atry-catch
block at all.
– Greg Burghardt
15 hours ago
2
"What is wrong with this ?" : nothing
– Ewan
14 hours ago
Take for instance a DB call. The SQL call fails and the exception bubbles up until it is handled and logged. At this point, I feel the logging would be worthless because I no longer have the parameters that caused it to fail. Imagine catching and logging at the level where the exception is thrown. Your investigation of the logged exception becomes almost too easy. Once you have logged the exception, set a flag in it's IDictionary Data property. Thenthrow
(NOTthrow(exception)
) the exception back up. Any catch blocks can check this flag usingwhen
filters to prevent multiple logging.
– Shai Cohen
3 hours ago
|
show 1 more comment
MethodA calls an MethodB which in turn calls MethodC.
There is NO exception handling in MethodB or MethodC. But there is exception handling in MethodA.
In MethodC an exception occurs.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
What is wrong with this ?
In my mind, at some point a caller will execute MethodB or MethodC, and when exceptions do occur in those methods, what will be gained from handling exceptions inside those methods, which essentially is just a try/catch/finally block instead of just let them bubble up to the callee ?
The statement or consensus around exception handling is to throw when execution cannot continue due to just that - an exception. I get that. But why not catch the exception further up the chain instead of having try/catch blocks all the way down.
I understand it when you need to free up resources, that's a different matter entirely.
design-patterns object-oriented-design exceptions
MethodA calls an MethodB which in turn calls MethodC.
There is NO exception handling in MethodB or MethodC. But there is exception handling in MethodA.
In MethodC an exception occurs.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
What is wrong with this ?
In my mind, at some point a caller will execute MethodB or MethodC, and when exceptions do occur in those methods, what will be gained from handling exceptions inside those methods, which essentially is just a try/catch/finally block instead of just let them bubble up to the callee ?
The statement or consensus around exception handling is to throw when execution cannot continue due to just that - an exception. I get that. But why not catch the exception further up the chain instead of having try/catch blocks all the way down.
I understand it when you need to free up resources, that's a different matter entirely.
design-patterns object-oriented-design exceptions
design-patterns object-oriented-design exceptions
edited 6 hours ago
cavpollo
1135
1135
asked 15 hours ago
Daniel FrostDaniel Frost
794
794
18
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
15 hours ago
With a good IDE and a proper coding style, you can know that some exception can be thrown when a method is called. Handle it or allow it to be propagated is the decision of the caller. I don't see any problem with this.
– Hieu Le
15 hours ago
3
If a method can't handle the exception, and is merely rethrowing it, I would say that is a code smell. If a method can't handle the exception, and doesn't need to do anything else when an exception is thrown then there is no need for atry-catch
block at all.
– Greg Burghardt
15 hours ago
2
"What is wrong with this ?" : nothing
– Ewan
14 hours ago
Take for instance a DB call. The SQL call fails and the exception bubbles up until it is handled and logged. At this point, I feel the logging would be worthless because I no longer have the parameters that caused it to fail. Imagine catching and logging at the level where the exception is thrown. Your investigation of the logged exception becomes almost too easy. Once you have logged the exception, set a flag in it's IDictionary Data property. Thenthrow
(NOTthrow(exception)
) the exception back up. Any catch blocks can check this flag usingwhen
filters to prevent multiple logging.
– Shai Cohen
3 hours ago
|
show 1 more comment
18
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
15 hours ago
With a good IDE and a proper coding style, you can know that some exception can be thrown when a method is called. Handle it or allow it to be propagated is the decision of the caller. I don't see any problem with this.
– Hieu Le
15 hours ago
3
If a method can't handle the exception, and is merely rethrowing it, I would say that is a code smell. If a method can't handle the exception, and doesn't need to do anything else when an exception is thrown then there is no need for atry-catch
block at all.
– Greg Burghardt
15 hours ago
2
"What is wrong with this ?" : nothing
– Ewan
14 hours ago
Take for instance a DB call. The SQL call fails and the exception bubbles up until it is handled and logged. At this point, I feel the logging would be worthless because I no longer have the parameters that caused it to fail. Imagine catching and logging at the level where the exception is thrown. Your investigation of the logged exception becomes almost too easy. Once you have logged the exception, set a flag in it's IDictionary Data property. Thenthrow
(NOTthrow(exception)
) the exception back up. Any catch blocks can check this flag usingwhen
filters to prevent multiple logging.
– Shai Cohen
3 hours ago
18
18
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
15 hours ago
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
15 hours ago
With a good IDE and a proper coding style, you can know that some exception can be thrown when a method is called. Handle it or allow it to be propagated is the decision of the caller. I don't see any problem with this.
– Hieu Le
15 hours ago
With a good IDE and a proper coding style, you can know that some exception can be thrown when a method is called. Handle it or allow it to be propagated is the decision of the caller. I don't see any problem with this.
– Hieu Le
15 hours ago
3
3
If a method can't handle the exception, and is merely rethrowing it, I would say that is a code smell. If a method can't handle the exception, and doesn't need to do anything else when an exception is thrown then there is no need for a
try-catch
block at all.– Greg Burghardt
15 hours ago
If a method can't handle the exception, and is merely rethrowing it, I would say that is a code smell. If a method can't handle the exception, and doesn't need to do anything else when an exception is thrown then there is no need for a
try-catch
block at all.– Greg Burghardt
15 hours ago
2
2
"What is wrong with this ?" : nothing
– Ewan
14 hours ago
"What is wrong with this ?" : nothing
– Ewan
14 hours ago
Take for instance a DB call. The SQL call fails and the exception bubbles up until it is handled and logged. At this point, I feel the logging would be worthless because I no longer have the parameters that caused it to fail. Imagine catching and logging at the level where the exception is thrown. Your investigation of the logged exception becomes almost too easy. Once you have logged the exception, set a flag in it's IDictionary Data property. Then
throw
(NOT throw(exception)
) the exception back up. Any catch blocks can check this flag using when
filters to prevent multiple logging.– Shai Cohen
3 hours ago
Take for instance a DB call. The SQL call fails and the exception bubbles up until it is handled and logged. At this point, I feel the logging would be worthless because I no longer have the parameters that caused it to fail. Imagine catching and logging at the level where the exception is thrown. Your investigation of the logged exception becomes almost too easy. Once you have logged the exception, set a flag in it's IDictionary Data property. Then
throw
(NOT throw(exception)
) the exception back up. Any catch blocks can check this flag using when
filters to prevent multiple logging.– Shai Cohen
3 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has no useful way to handle it, then it should allow the exception to propagate up to MethodA.
The only reasons why a method should have a catch and rethrow mechanism are:
- You want to convert one exception to a different one that is more meaningful to the caller above.
- You want to add extra information to the exception.
- You need a catch clause to clean up resources that would be leaked without one.
Otherwise, catching exceptions at the wrong level tends to result in code that silently fails without providing any useful feedback to the calling code (and ultimately the user of the software). The alternative of catching an exception and then immediately rethrowing it is pointless.
11
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
14 hours ago
9
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
2
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
1
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.
– Colin Young
11 hours ago
1
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
|
show 4 more comments
What is wrong with this ?
Absolutely nothing.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
"handles it appropriately" is the important part. That's the crux of Structured Exception Handling.
If your code can do something "useful" with an Exception, go for it. If not, then let well alone.
. . . why not catch the exception further up the chain instead of having try/catch blocks all the way down.
That's exactly what you should be doing.
If you're reading code that has handler/rethrowers "all the way down", then you're [probably] reading some pretty poor code.
Sadly, some Developers just see catch blocks as "boiler-plate" code that they throw in (no pun intended) to every method they write, often because they don't really "get" Exception Handling and think they have to add something so that Exceptions don't "escape" and kill their program.
Part of the difficulty here is that, most of the time, this problem won't even get noticed, because Exceptions aren't being thrown all the time but, when they are, the program is going to waste an awful lot of time and effort gradually unpicking the call stack to get up to somewhere that actually does something useful with the Exception.
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "131"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391654%2fexception-propagation-when-to-catch-exceptions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has no useful way to handle it, then it should allow the exception to propagate up to MethodA.
The only reasons why a method should have a catch and rethrow mechanism are:
- You want to convert one exception to a different one that is more meaningful to the caller above.
- You want to add extra information to the exception.
- You need a catch clause to clean up resources that would be leaked without one.
Otherwise, catching exceptions at the wrong level tends to result in code that silently fails without providing any useful feedback to the calling code (and ultimately the user of the software). The alternative of catching an exception and then immediately rethrowing it is pointless.
11
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
14 hours ago
9
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
2
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
1
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.
– Colin Young
11 hours ago
1
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
|
show 4 more comments
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has no useful way to handle it, then it should allow the exception to propagate up to MethodA.
The only reasons why a method should have a catch and rethrow mechanism are:
- You want to convert one exception to a different one that is more meaningful to the caller above.
- You want to add extra information to the exception.
- You need a catch clause to clean up resources that would be leaked without one.
Otherwise, catching exceptions at the wrong level tends to result in code that silently fails without providing any useful feedback to the calling code (and ultimately the user of the software). The alternative of catching an exception and then immediately rethrowing it is pointless.
11
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
14 hours ago
9
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
2
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
1
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.
– Colin Young
11 hours ago
1
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
|
show 4 more comments
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has no useful way to handle it, then it should allow the exception to propagate up to MethodA.
The only reasons why a method should have a catch and rethrow mechanism are:
- You want to convert one exception to a different one that is more meaningful to the caller above.
- You want to add extra information to the exception.
- You need a catch clause to clean up resources that would be leaked without one.
Otherwise, catching exceptions at the wrong level tends to result in code that silently fails without providing any useful feedback to the calling code (and ultimately the user of the software). The alternative of catching an exception and then immediately rethrowing it is pointless.
As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has no useful way to handle it, then it should allow the exception to propagate up to MethodA.
The only reasons why a method should have a catch and rethrow mechanism are:
- You want to convert one exception to a different one that is more meaningful to the caller above.
- You want to add extra information to the exception.
- You need a catch clause to clean up resources that would be leaked without one.
Otherwise, catching exceptions at the wrong level tends to result in code that silently fails without providing any useful feedback to the calling code (and ultimately the user of the software). The alternative of catching an exception and then immediately rethrowing it is pointless.
edited 12 hours ago
answered 15 hours ago
Simon BSimon B
4,74311628
4,74311628
11
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
14 hours ago
9
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
2
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
1
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.
– Colin Young
11 hours ago
1
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
|
show 4 more comments
11
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
14 hours ago
9
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
2
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
1
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.
– Colin Young
11 hours ago
1
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
11
11
@GregBurghardt if your language has something akin to
try ... finally ...
, then use that, not catch & rethrow– Caleth
14 hours ago
@GregBurghardt if your language has something akin to
try ... finally ...
, then use that, not catch & rethrow– Caleth
14 hours ago
9
9
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
"catching an exception and then immediately rethrowing it is pointless" Depending on the language and how you go about this it can be actively harmful to the codebase. Often people attempting this remove a lot of information about the exception such as the original stacktrace. I've dealt with code where the caller gets an exception that is completely misleading as to what happened and where.
– JimmyJames
12 hours ago
2
2
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
Whatever you do, don't throw away the original exception information. Make sure it is wrapped in the new exception so it is available to whoever ends up debugging some issue months or years don't the road. There's nothing like the joy of debugging an application that uses a framework whose guiding principle was "the users of this are too stupid to be trusted with complicated things like exceptions" and not being able to see anything other than some generic exception from 5 layers above the true, original problem.
– Colin Young
11 hours ago
1
1
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.
LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.– Colin Young
11 hours ago
@Voo In your example you do know what to do with it. Wrap it in a documented exception specific to your code, e.g.
LoadDataException
and include the original exception details according to your language features, so that future maintainers are able to see the root cause without having to attach a debugger and figure out how to reproduce the problem.– Colin Young
11 hours ago
1
1
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
@Voo You seem to have missed the "You want to convert one exception to a different one that is more meaningful to the caller above" reason for catch/rethrow scenarios.
– jpmc26
9 hours ago
|
show 4 more comments
What is wrong with this ?
Absolutely nothing.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
"handles it appropriately" is the important part. That's the crux of Structured Exception Handling.
If your code can do something "useful" with an Exception, go for it. If not, then let well alone.
. . . why not catch the exception further up the chain instead of having try/catch blocks all the way down.
That's exactly what you should be doing.
If you're reading code that has handler/rethrowers "all the way down", then you're [probably] reading some pretty poor code.
Sadly, some Developers just see catch blocks as "boiler-plate" code that they throw in (no pun intended) to every method they write, often because they don't really "get" Exception Handling and think they have to add something so that Exceptions don't "escape" and kill their program.
Part of the difficulty here is that, most of the time, this problem won't even get noticed, because Exceptions aren't being thrown all the time but, when they are, the program is going to waste an awful lot of time and effort gradually unpicking the call stack to get up to somewhere that actually does something useful with the Exception.
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
add a comment |
What is wrong with this ?
Absolutely nothing.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
"handles it appropriately" is the important part. That's the crux of Structured Exception Handling.
If your code can do something "useful" with an Exception, go for it. If not, then let well alone.
. . . why not catch the exception further up the chain instead of having try/catch blocks all the way down.
That's exactly what you should be doing.
If you're reading code that has handler/rethrowers "all the way down", then you're [probably] reading some pretty poor code.
Sadly, some Developers just see catch blocks as "boiler-plate" code that they throw in (no pun intended) to every method they write, often because they don't really "get" Exception Handling and think they have to add something so that Exceptions don't "escape" and kill their program.
Part of the difficulty here is that, most of the time, this problem won't even get noticed, because Exceptions aren't being thrown all the time but, when they are, the program is going to waste an awful lot of time and effort gradually unpicking the call stack to get up to somewhere that actually does something useful with the Exception.
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
add a comment |
What is wrong with this ?
Absolutely nothing.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
"handles it appropriately" is the important part. That's the crux of Structured Exception Handling.
If your code can do something "useful" with an Exception, go for it. If not, then let well alone.
. . . why not catch the exception further up the chain instead of having try/catch blocks all the way down.
That's exactly what you should be doing.
If you're reading code that has handler/rethrowers "all the way down", then you're [probably] reading some pretty poor code.
Sadly, some Developers just see catch blocks as "boiler-plate" code that they throw in (no pun intended) to every method they write, often because they don't really "get" Exception Handling and think they have to add something so that Exceptions don't "escape" and kill their program.
Part of the difficulty here is that, most of the time, this problem won't even get noticed, because Exceptions aren't being thrown all the time but, when they are, the program is going to waste an awful lot of time and effort gradually unpicking the call stack to get up to somewhere that actually does something useful with the Exception.
What is wrong with this ?
Absolutely nothing.
Now, that exception is bubbling up to MethodA, which handles it appropriately.
"handles it appropriately" is the important part. That's the crux of Structured Exception Handling.
If your code can do something "useful" with an Exception, go for it. If not, then let well alone.
. . . why not catch the exception further up the chain instead of having try/catch blocks all the way down.
That's exactly what you should be doing.
If you're reading code that has handler/rethrowers "all the way down", then you're [probably] reading some pretty poor code.
Sadly, some Developers just see catch blocks as "boiler-plate" code that they throw in (no pun intended) to every method they write, often because they don't really "get" Exception Handling and think they have to add something so that Exceptions don't "escape" and kill their program.
Part of the difficulty here is that, most of the time, this problem won't even get noticed, because Exceptions aren't being thrown all the time but, when they are, the program is going to waste an awful lot of time and effort gradually unpicking the call stack to get up to somewhere that actually does something useful with the Exception.
answered 11 hours ago
Phill W.Phill W.
8,0283927
8,0283927
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
add a comment |
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
What's even worse is when the application catches the exception, and then logs it (where hopefully it won't just sit there forever) and attempts to continue on as usual, even when it really can't.
– Solomon Ucko
10 hours ago
add a comment |
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391654%2fexception-propagation-when-to-catch-exceptions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
18
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
15 hours ago
With a good IDE and a proper coding style, you can know that some exception can be thrown when a method is called. Handle it or allow it to be propagated is the decision of the caller. I don't see any problem with this.
– Hieu Le
15 hours ago
3
If a method can't handle the exception, and is merely rethrowing it, I would say that is a code smell. If a method can't handle the exception, and doesn't need to do anything else when an exception is thrown then there is no need for a
try-catch
block at all.– Greg Burghardt
15 hours ago
2
"What is wrong with this ?" : nothing
– Ewan
14 hours ago
Take for instance a DB call. The SQL call fails and the exception bubbles up until it is handled and logged. At this point, I feel the logging would be worthless because I no longer have the parameters that caused it to fail. Imagine catching and logging at the level where the exception is thrown. Your investigation of the logged exception becomes almost too easy. Once you have logged the exception, set a flag in it's IDictionary Data property. Then
throw
(NOTthrow(exception)
) the exception back up. Any catch blocks can check this flag usingwhen
filters to prevent multiple logging.– Shai Cohen
3 hours ago