471,317 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Catch in procedure with out return

Hi
I wanted to write a general procedure to handle all my errors.
My idea was to place various stuff in the error handler then call it,
from all my "Catch" blicks, ending it the Exception.

public whatever Procedure_1()
.....
catch(Exception er)
{
CallErrHandler(er);
}

.....

The error handler routine would actually re-throw the exception after
doing its other tasks (logging, re-wording, etc)
So, If an error occurs, there will never be a "return" from the
Procedure_1.
The compiler though, requires a return clause.

Is there any way around this?
Thanks
Jeff
Jan 23 '06 #1
5 1133
Jeff User wrote:
I wanted to write a general procedure to handle all my errors.
My idea was to place various stuff in the error handler then call it,
from all my "Catch" blicks, ending it the Exception.
<snip>
The error handler routine would actually re-throw the exception after
doing its other tasks (logging, re-wording, etc)
So, If an error occurs, there will never be a "return" from the
Procedure_1.
The compiler though, requires a return clause.

Is there any way around this?


Yes, although it's not nice:

CallErrHandler(...);
// Compiler doesn't know we never get this far
return null;

It's nasty, but it works. It doesn't help your coverage data though :(

If you're using C# 2.0, you could make CallErrHandler a generic method,
something like:

public T CallErrHandler<T> (parameters as before)
{
// Code as before
return default(T);
}

And then make your code "return" the value returned from
CallErrHandler<T>, using an appropriate version for your call. You may
have to work to persuade the compiler that it'll get as far as the
return value in CallErrHandler though!

Jon

Jan 23 '06 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

If you're using C# 2.0, you could make CallErrHandler a generic method,
something like:

public T CallErrHandler<T> (parameters as before)
{
// Code as before
return default(T);
}

And then make your code "return" the value returned from
CallErrHandler<T>, using an appropriate version for your call.


I ask this out of sheer ignorance: can T be "void" here? It could in C++,
where:

void B();
void A()
{
return B();
}

was made legal to help templates do things like your suggestion.
(Parenthetically, I think the above code is perfectly sensible even in the
absence of generics/templates.)
Jan 24 '06 #3
Mike Schilling wrote:
And then make your code "return" the value returned from
CallErrHandler<T>, using an appropriate version for your call.


I ask this out of sheer ignorance: can T be "void" here? It could in C++,
where:

void B();
void A()
{
return B();
}

was made legal to help templates do things like your suggestion.
(Parenthetically, I think the above code is perfectly sensible even in the
absence of generics/templates.)


I don't *think* so, but I'd have to check. I don't know what the value
of default(void) would be...

Of course, it wouldn't actually matter if the method you're in the
middle of returned void - you could just call CallErrHandler and ignore
the return value.

Jon

Jan 24 '06 #4

The error handler routine would actually re-throw the exception after
doing its other tasks (logging, re-wording, etc)
So, If an error occurs, there will never be a "return" from the
Procedure_1.
The compiler though, requires a return clause.

Is there any way around this?


Hi,

hope you understand the problem so I will not explain it.

solution: IF the error handler will throw an exception what if you recode it
to looks like:

public Exception CallErrHandler( Exception e) { ... }

then you can code it like

catch(Exception er)
{
throw CallErrHandler(er);
}

The compiler will not complain and the semantics is clear enough.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 24 '06 #5
Jeff User wrote:
Hi
I wanted to write a general procedure to handle all my errors.
My idea was to place various stuff in the error handler then call it,
from all my "Catch" blicks, ending it the Exception.

public whatever Procedure_1()
....
catch(Exception er)
{
CallErrHandler(er);
}

....

The error handler routine would actually re-throw the exception after
doing its other tasks (logging, re-wording, etc)
So, If an error occurs, there will never be a "return" from the
Procedure_1.
The compiler though, requires a return clause.

Is there any way around this?


I have one more work around suggestion.

catch(Exception er)
{
CallErrHandler(er);
throw; // this statement will never be reached. CallErrHandler throws
exception for us.
}

by the way, when you rethrow the exception in CallErrHandler, it messes up
the stack trace.

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Jan 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Evgeny Gopengauz | last post: by
2 posts views Thread by Dino L. | last post: by
4 posts views Thread by Jeff User | last post: by
4 posts views Thread by jy836 | last post: by
32 posts views Thread by cj | last post: by
3 posts views Thread by JJ297 | last post: by
11 posts views Thread by darrel | last post: by
reply views Thread by rosydwin | last post: by

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.