473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Throwing exception from private methods

Sek
Hi Folks!

I was pondering over a code and noticed that exception handlers were
present in the private, protected as well as public methods. And,
ofcourse, public methods were calling priv/prot methods internally.

My thought was, the exception is being rethrown and propagated by the
non-public methods to the public methods, causing performance overhead
(stack winding etc).
I do agree that, the purpose of throwing the exception by internal
methods and the need for such exception in public methods is the
deciding factor.

But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).

So, wouldn't it be appropriate to have try-catch handlers in public
methods alone and avoid the catch-rethrow practice in non-public
methods?

TIA>
Sek.

Oct 11 '06 #1
7 5069
"Sek" <se****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
[...]
But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).

So, wouldn't it be appropriate to have try-catch handlers in public
methods alone and avoid the catch-rethrow practice in non-public
methods?
Personally, I don't see how the choice of handling exceptions has anything
to do with the access level of a method.

The reason to handle an exception is if you can do something about it or
need to respond to it. This isn't related at all to whether a method is
public, private, or whatever. It just has to do with the nature of the
exception and what the method is doing.

Generally speaking the situations in which an exception is caught and then
rethrown is when the code needs to inspect the exception to see if it can
handle it (some exceptions may be fatal while others are not...the fatal
ones get rethrown), or if the code needs to explicitly clean something up
when an exception occurs (this happens a lot less in C# than it would have
in the old C days, but it can still happen). These are not situations in
which it's possible to just remove the exception handling in the code.

Pete
Oct 11 '06 #2
Hi,

Peter Duniho wrote:
"Sek" <se****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>[...]
But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).

So, wouldn't it be appropriate to have try-catch handlers in public
methods alone and avoid the catch-rethrow practice in non-public
methods?

Personally, I don't see how the choice of handling exceptions has anything
to do with the access level of a method.

The reason to handle an exception is if you can do something about it or
need to respond to it. This isn't related at all to whether a method is
public, private, or whatever. It just has to do with the nature of the
exception and what the method is doing.

Generally speaking the situations in which an exception is caught and then
rethrown is when the code needs to inspect the exception to see if it can
handle it (some exceptions may be fatal while others are not...the fatal
ones get rethrown), or if the code needs to explicitly clean something up
when an exception occurs (this happens a lot less in C# than it would have
in the old C days, but it can still happen). These are not situations in
which it's possible to just remove the exception handling in the code.

Pete
Also, when you rethrow, make sure to use "throw;" and avoid "throw ex;"

See
http://www.galasoft-lb.ch/mydotnet/a...006092901.aspx

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 11 '06 #3
Hi,

"Sek" <se****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi Folks!

I was pondering over a code and noticed that exception handlers were
present in the private, protected as well as public methods. And,
ofcourse, public methods were calling priv/prot methods internally.
Nothing weird here
My thought was, the exception is being rethrown and propagated by the
non-public methods to the public methods, causing performance overhead
(stack winding etc).
an exception has some overhead associate, of course it should not have a big
impact, they after all occur only in "exceptional" cirscuntances.

But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).
Not at all, the visibility of a method has nothing to do with how it handle
exceptions.

Oct 11 '06 #4

Laurent Bugnion wrote:
Hi,

Peter Duniho wrote:
"Sek" <se****@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
[...]
But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).

So, wouldn't it be appropriate to have try-catch handlers in public
methods alone and avoid the catch-rethrow practice in non-public
methods?
Personally, I don't see how the choice of handling exceptions has anything
to do with the access level of a method.

The reason to handle an exception is if you can do something about it or
need to respond to it. This isn't related at all to whether a method is
public, private, or whatever. It just has to do with the nature of the
exception and what the method is doing.

Generally speaking the situations in which an exception is caught and then
rethrown is when the code needs to inspect the exception to see if it can
handle it (some exceptions may be fatal while others are not...the fatal
ones get rethrown), or if the code needs to explicitly clean something up
when an exception occurs (this happens a lot less in C# than it would have
in the old C days, but it can still happen). These are not situations in
which it's possible to just remove the exception handling in the code.

Pete

Also, when you rethrow, make sure to use "throw;" and avoid "throw ex;"

See
http://www.galasoft-lb.ch/mydotnet/a...006092901.aspx
That very much depends. Often you need to transform the exception from
the lower-level method because it makes no sense in the context of your
higher-level method. For example, a lower-level method may throw an
ArgumentException, but you may want to translate that into a different
ArgumentException referring to one of your arguments.

If you're going to throw a new exception, it should wrap the one you
received as an inner exception.

Oct 11 '06 #5
Hi,

Bruce Wood wrote:
Laurent Bugnion wrote:
>>
See
http://www.galasoft-lb.ch/mydotnet/a...006092901.aspx

That very much depends. Often you need to transform the exception from
the lower-level method because it makes no sense in the context of your
higher-level method. For example, a lower-level method may throw an
ArgumentException, but you may want to translate that into a different
ArgumentException referring to one of your arguments.

If you're going to throw a new exception, it should wrap the one you
received as an inner exception.
Isn't it precisely what this sentence means:

"Of course, another better known option is to wrap the caught exception
in another one, using the Exception.InnerException property."

:-)

Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 11 '06 #6

Sek wrote:
I was pondering over a code and noticed that exception handlers were
present in the private, protected as well as public methods. And,
ofcourse, public methods were calling priv/prot methods internally.

My thought was, the exception is being rethrown and propagated by the
non-public methods to the public methods, causing performance overhead
(stack winding etc).
I do agree that, the purpose of throwing the exception by internal
methods and the need for such exception in public methods is the
deciding factor.
I have no idea what you mean by that last sentence. A method will throw
an exception when something happens that its author decided as
"exceptional" and not part of the normal functioning of the method.
Someone passing an invalid argument is a good example.

The difficulty is that when writing a library method that will be used
on other projects, it's sometimes difficult (or impossible) to decide
what constitutes "exceptional". What may be exceptional to one
application may be business-as-usual for another. In situations like
this, you have three choices:
1. Code the error as a return value and therefore assume that all
callers will check the return. This is old-school but it is perfectly
valid in C# and used by the .NET Framework in many places.
2. Code the error as an exception and have any callers that want it to
be business-as-usual catch the exception.
3. Code the error as an exception and provide another routine to check
pre-conditions, so that callers who consider the error
business-as-usual can call the check routine first. (This is done in
the .NET Framework 2.0 with Parse() and TryParse().)

Fortunately, these situations are relatively rare. It's usually pretty
clear what constitutes an exceptional situation and what doesn't, which
is why most C# code should have very few catch clauses.
But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.
It's better to avoid try-catch everywhere, except when they add value.
You should be catching exceptions only when you know you can do
something useful with them.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).
Good grief, no! There is no "safety" in catching an exception with
which you can do nothing useful. Let it bubble up the stack and crash
your program, and write global exception handlers to log such
exceptions. If you have try...catch in every non-public method (or in
every method), all you're doing is obscuring errors in your
application.
So, wouldn't it be appropriate to have try-catch handlers in public
methods alone and avoid the catch-rethrow practice in non-public
methods?
Catch-rethrow has its uses, but it certainly shouldn't be applied
willynilly. Catch-rethrow is useful when:
1. The exception you're catching makes no sense in the context of your
method. That is, a lower-level method throws an exception that makes
sense because of what that method does, but it wouldn't make any sense
to your caller, so you catch it, wrap it in a more appropriate type of
exception, and throw that.
2. You can add valuable information about your context that will help
debug problems. Again, catch the exception, wrap it in another
exception with more information, and throw the new exception.
3. You want to inspect the exception to see if it's one that you can
handle. This is usually based on the exception's type, and thus the
catch clause, but sometimes it isn't, and you may decide to handle the
exception, or rethrow it, in which case use throw, not throw ex.
4. You want to throw it later. For example, you may be doing 100
operations, and #54 fails. You may want to store that exception and
re-throw it after all operations complete, most likely wrapping it in
another exception in the process.

....and probably more I haven't thought of. However, one doesn't catch
and re-throw "just because".

Oct 11 '06 #7
Sek
Thanks to all the folks! Special thanks to Bruce Wood :)

To Summarise:
DO's:
- Catch-rethrow to be used when the caught exception has to be
transformed/decorated for the next handler
- While rethrowing the exception as such without any modification,
prefer "throw;" compared to "throw ex;"

DONT's:
- Using try-catch-rethrow as a precautionary safegaurd(against
unexpected scenarios) around every piece of code written
- Simply propagating/rethrowing the caught exception to next level
handler without any modification/decoration
On Oct 12, 1:00 am, "Bruce Wood" <brucew...@canada.comwrote:
Sek wrote:
I was pondering over a code and noticed that exception handlers were
present in the private, protected as well as public methods. And,
ofcourse, public methods were calling priv/prot methods internally.
My thought was, the exception is being rethrown and propagated by the
non-public methods to the public methods, causing performance overhead
(stack winding etc).
I do agree that, the purpose of throwing the exception by internal
methods and the need for such exception in public methods is the
deciding factor.I have no idea what you mean by that last sentence. A method will throw
an exception when something happens that its author decided as
"exceptional" and not part of the normal functioning of the method.
Someone passing an invalid argument is a good example.

The difficulty is that when writing a library method that will be used
on other projects, it's sometimes difficult (or impossible) to decide
what constitutes "exceptional". What may be exceptional to one
application may be business-as-usual for another. In situations like
this, you have three choices:
1. Code the error as a return value and therefore assume that all
callers will check the return. This is old-school but it is perfectly
valid in C# and used by the .NET Framework in many places.
2. Code the error as an exception and have any callers that want it to
be business-as-usual catch the exception.
3. Code the error as an exception and provide another routine to check
pre-conditions, so that callers who consider the error
business-as-usual can call the check routine first. (This is done in
the .NET Framework 2.0 with Parse() and TryParse().)

Fortunately, these situations are relatively rare. It's usually pretty
clear what constitutes an exceptional situation and what doesn't, which
is why most C# code should have very few catch clauses.
But, won't it be better to avoid using try-catch handlers in non-public
methods and rather handle these exceptions only in public methods.It's better to avoid try-catch everywhere, except when they add value.
You should be catching exceptions only when you know you can do
something useful with them.
Commonly, folks do use try-catch handler in every non-public
method(generally thought of as a safety measure!).Good grief, no! There is no "safety" in catching an exception with
which you can do nothing useful. Let it bubble up the stack and crash
your program, and write global exception handlers to log such
exceptions. If you have try...catch in every non-public method (or in
every method), all you're doing is obscuring errors in your
application.
So, wouldn't it be appropriate to have try-catch handlers in public
methods alone and avoid the catch-rethrow practice in non-public
methods?Catch-rethrow has its uses, but it certainly shouldn't be applied
willynilly. Catch-rethrow is useful when:
1. The exception you're catching makes no sense in the context of your
method. That is, a lower-level method throws an exception that makes
sense because of what that method does, but it wouldn't make any sense
to your caller, so you catch it, wrap it in a more appropriate type of
exception, and throw that.
2. You can add valuable information about your context that will help
debug problems. Again, catch the exception, wrap it in another
exception with more information, and throw the new exception.
3. You want to inspect the exception to see if it's one that you can
handle. This is usually based on the exception's type, and thus the
catch clause, but sometimes it isn't, and you may decide to handle the
exception, or rethrow it, in which case use throw, not throw ex.
4. You want to throw it later. For example, you may be doing 100
operations, and #54 fails. You may want to store that exception and
re-throw it after all operations complete, most likely wrapping it in
another exception in the process.

...and probably more I haven't thought of. However, one doesn't catch
and re-throw "just because".
Oct 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
4
by: Terry | last post by:
Hello, I am trying to get a response for an .aspx page in my current project (same virtual directory) by using WebRequest.GetResponse but I keep getting a exception with "500 Internal server...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
5
by: GG | last post by:
Any thoughts on designing classes wheather to throw the exception and the consumer has to capture it on try catch or wrap all the methods in a main method This is the wrap logic MainMethod{ bool...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
3
balabaster
by: balabaster | last post by:
Hey all, this might seem somewhat remedial but I'm unable to find any documentation on it - it pertains to throwing exceptions within a DLL. I've written a DLL that contains a bunch of classes....
3
by: Kevinst | last post by:
Hello, I have a c# com object that exposes several methods. This com object is to be used in vbscripts as well as vba scripts. In my application error handling is done by throwing different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.