K
Kevin Yu
is it a bad programming design to throw exception in the try block then
catch it??
catch it??
Kevin said:is it a bad programming design to throw exception in the try block then
catch it??
Kevin Yu said:is it a bad programming design to throw exception in the try block then
catch it??
Miha Markic said:Hi Kevin,
Depends. For example, if you have a deeply nested procedure, throwing an
exception to exit might be a viable way.
However, normally throwing an exception is costly plus throwing an exception
should signal an error.
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info
Kevin Yu said:is it a bad programming design to throw exception in the try block then
catch it??
Helge Jensen said:Kevin said:is it a bad programming design to throw exception in the try block then
catch it??
Here are some guidelines that i have formulated after having tried
different strategies of exceptions:
Don't use exceptions to indicate errors you expect to occur.
Don't throw exceptions to "break" the program flow to a specific
catch-handler (goto):
try {
...
if ( unexpectedSituation )
throw new UnexpectedSituation();
...
} catch ( UnexpectedSituation ) {
// this is a complicated way to do goto
}
But if an unhandled exceptional situation occurs, then do what you would
normally do, for eample:
Stream s = new Stream("x");
try {
byte[] data = new byte[4];
int read = 0;
while ( read < data.Length ) {
int r = s.Read(data, read, data.Length - read);
if ( r == 0 )
throw new ParseError("Stream ended before data could be read");
}
return data;
} catch ( InvalidOperationError ) { // unrelated to ParseError
// Reading failed!
} finally {
s.Close();
}
NOTE: you could use: "using (Stream s = new Stream("x")) { ... }"
instead of try/catch.
It is often good to write the code to have minimal try/catch-blocks, for
example if there is a risk that dict[key] may not be a Foo:
Foo foo = (Foo)dict[key];
try {
foo.f(...);
} catch ( FooProcessingException ) {
... // sensible error handler
}
instead of:
try {
((Foo)dict[key]).f(...);
} catch ( FooProcessingException ) {
... // sensible error handler
}
So that error-handling is narrowly applied to the code that is expected
to expose the error.
In general: only catch if you can actually do anyting to remedy the
error, or is at the top of the call-stack. A possible exception is
catching for logging and retrow:
try {
f(...);
catch ( Exception e ) {
Log(e);
throw;
}
--
Helge Jensen
mailto:[email protected]
sip:[email protected]
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Miha Markic said:Hi Kevin,
Depends. For example, if you have a deeply nested procedure, throwing an
exception to exit might be a viable way.
However, normally throwing an exception is costly plus throwing an
exception should signal an error.
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
SLODUG - Slovene Developer Users Group www.codezone-si.info
Kevin Yu said:is it a bad programming design to throw exception in the try block then
catch it??
Jeff Louie said:My opinion is that this can be used sparingly when it makes the code
flow
easier to read. So if I have a catch that basically returns false on an
error, sets
an error message etc. and I have a invariant error that should also
return
false, then it is very simple to throw an exception on an invariant
error and
just reuse the code block in the catch. In other words, I think
throwing an
exception in the try block is acceptable when it makes the code easier
to read
and follow and understand what is going on. Else I don't throw
exceptions in
try.
Kevin Yu said:throwing an exception in the try block is costly, it slows down the
prog, yea, it make the code easier to read, I think with a better
refactory technique such design can be avoided. in my case ( the code
I am reading) it's messy because even in the catch block, and try
catch block is also there in case of the exception occur in the catch
block from writing the error to eh event log. I mean how many level
of exception handling u need.
Mohammad said:Helge,
If you don't mind, I would like to add that, from my own experience, I
have yet to come across a case where anything can be done to remedy an
error flagged by an excpetion.
that is actually doable is in the case of parsing strings to other data
types, in which case the error can be corrected by setting the target
variable to some sensible default.
So, again from my own experience, the best thing to do in the case of
an exception is to simply roll back any changes done within the scope
of the operation and letting the excpetion bubble up to the caller.
This can be accomplished without writing any exception handling code by
borrowing the Resource Initlializing is Acquisition (RIIA) idiom from
C++, which is somewhat easy to implement in C# 2.0, as can be seen from
this article: http://www.codeproject.com/csharp/exceptions_generics.asp
I would also like to add that I don't believe that catching exceptions
just to wrap them in custom exceptions and them rethrow them is a good
idea. Same for logging excpetions, which should be done in one place
and one place only for the whole program.
Kevin said:basically he wrap everything in try catch block even when instantiating a
new object, he will check for if the newly created object is null, then
throw and exception, I mean if the new operate fail to instantiate an
instance aka it's out of memory, the runtime will throw and
OutofMemoryException, the guys is clearly came from a C++ background.
anyway, is it a good idea to wrap everything inside a try catch block, I
read somewhere that try block doesn't cost anything, only when exception
occur, it will cost u, so does that mean it's a good idea to wrap everything
in try catch, I mean hack, this will make the system *Robus*.
enlighten me.
As an example of how overblown the issue of the performance penalty for
exceptions is, on another group a while ago someone was asking whether
or not his application was suffering because it was throwing a couple
of hundred exceptions per *hour*. Many people said that yes, this was
probably slowing his application down significantly.
Kevin Yu said:John
don't get me wrong, I just want some justice on someone else's code that I
need to follow and support.
basically he wrap everything in try catch block even when instantiating a
new object, he will check for if the newly created object is null, then
throw and exception, I mean if the new operate fail to instantiate an
instance aka it's out of memory, the runtime will throw and
OutofMemoryException, the guys is clearly came from a C++ background.
anyway, is it a good idea to wrap everything inside a try catch block, I
read somewhere that try block doesn't cost anything, only when exception
occur, it will cost u, so does that mean it's a good idea to wrap
everything
Kevin Yu said:hi All
After all these discussions, I only have couple of questions here.
1. is it a good idea to wrap everything in try catch block? explain in
evidence/examples please.
2. is throwing exception in a try catch block a good idea? can Goto be an
alternative??
3. is rethrowing an exception a good design pattern or necessary? I mean
instead of returning a boolean