Exception propagation: When to catch exceptions?Using 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 to find the tex encoding of specific fonts?
Why is the Sun made of light elements only?
What's the "magic similar to the Knock spell" referenced in the Dungeon of the Mad Mage adventure?
Why do unstable nuclei form?
Names of the Six Tastes
No such column 'DeveloperName' on entity 'RecordType' after Summer '19 release on sandbox
How are one-time password generators like Google Authenticator different from having two passwords?
Is ‘despite that’ right?
Examples where existence is harder than evaluation
Would encrypting a database protect against a compromised admin account?
How can a demonic viral infection spread throughout the body without being noticed?
Are there variations of the regular runtimes of the Big-O-Notation?
Removing all characters except digits from clipboard
What is wrong with my code? RGB potentiometer
Why do Thanos' punches not kill Captain America or at least cause vital wounds?
Is every story set in the future "science fiction"?
A Cunning Riley Riddle
How to efficiently lower your karma
Why are parallelograms defined as quadrilaterals? What term would encompass polygons with greater than two parallel pairs?
Extending Kan fibrations, without using minimal fibrations
Company threw a surprise party for the CEO, 3 weeks later management says we have to pay for it, do I have to?
What can cause an unfrozen indoor copper drain pipe to crack?
What's the difference between const array and static const array in C/C++
Pre-1993 comic in which Wolverine's claws were turned to rubber?
Exception propagation: When to catch exceptions?
Using 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
add a 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
16
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
11 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
10 hours ago
2
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
10 hours ago
1
"What is wrong with this ?" : nothing
– Ewan
10 hours ago
2
Worth noting that the formal term for this is "exception propagation".
– Kyle McVay
7 hours ago
add a 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 1 hour ago
cavpollo
1135
1135
asked 11 hours ago
Daniel FrostDaniel Frost
644
644
16
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
11 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
10 hours ago
2
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
10 hours ago
1
"What is wrong with this ?" : nothing
– Ewan
10 hours ago
2
Worth noting that the formal term for this is "exception propagation".
– Kyle McVay
7 hours ago
add a comment |
16
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
11 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
10 hours ago
2
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
10 hours ago
1
"What is wrong with this ?" : nothing
– Ewan
10 hours ago
2
Worth noting that the formal term for this is "exception propagation".
– Kyle McVay
7 hours ago
16
16
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
11 hours ago
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
11 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
10 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
10 hours ago
2
2
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
10 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
10 hours ago
1
1
"What is wrong with this ?" : nothing
– Ewan
10 hours ago
"What is wrong with this ?" : nothing
– Ewan
10 hours ago
2
2
Worth noting that the formal term for this is "exception propagation".
– Kyle McVay
7 hours ago
Worth noting that the formal term for this is "exception propagation".
– Kyle McVay
7 hours ago
add a 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.
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
9
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
10 hours ago
6
"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
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.
– Laiv
7 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
6 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
6 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.
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
9
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
10 hours ago
6
"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
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.
– Laiv
7 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
6 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.
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
9
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
10 hours ago
6
"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
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.
– Laiv
7 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
6 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 8 hours ago
answered 10 hours ago
Simon BSimon B
4,70311628
4,70311628
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
9
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
10 hours ago
6
"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
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.
– Laiv
7 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
6 hours ago
|
show 4 more comments
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
9
@GregBurghardt if your language has something akin totry ... finally ...
, then use that, not catch & rethrow
– Caleth
10 hours ago
6
"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
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.
– Laiv
7 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
6 hours ago
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
I would also add "cleaning up resources" as another reason to catch and then rethrow.
– Greg Burghardt
10 hours ago
9
9
@GregBurghardt if your language has something akin to
try ... finally ...
, then use that, not catch & rethrow– Caleth
10 hours ago
@GregBurghardt if your language has something akin to
try ... finally ...
, then use that, not catch & rethrow– Caleth
10 hours ago
6
6
"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
7 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
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.– Laiv
7 hours ago
You want to convert one exception to a different one
or just turning the checked one into a non-checked.– Laiv
7 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
6 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
6 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
6 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
6 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 7 hours ago
Phill W.Phill W.
7,9983927
7,9983927
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
6 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
6 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
6 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
6 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
16
Why do you think the consensus is to have a chain of pass through catches?
– Caleth
11 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
10 hours ago
2
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
10 hours ago
1
"What is wrong with this ?" : nothing
– Ewan
10 hours ago
2
Worth noting that the formal term for this is "exception propagation".
– Kyle McVay
7 hours ago