471,315 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Throwing exceptions created with reflection

Hi,

Does someone know whether it is a bad practice to create exception
objects through reflection and throw them? I want to use this in order
to dynamically convert from an integer errorCode to an exception (via a
map errorCode -> typeof( Exception))?

Assembly excAssembly = _exceptionType.Assembly;
Object [] args = new Object [ 1] { _errorCode };
Exception e =
(Exception)excAssembly.CreateInstance( _exceptionType.FullName,

false,

BindingFlags.CreateInstance,

null,

args,

null,

null);
throw (e);

Thanks in advance,
Kaloian.

Dec 30 '05 #1
3 1720
Kaloian,

Well, what are your concerns? I mean, when you throw the exception, you
just throw it. Granted, the idea of mapping an integer code to an exception
is a little odd to me, since I think that the integer code is enough (and
you don't necessarily have to throw an exception).

I think there is a difference between an error code and an exception,
but in the end, it's up to you.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ka******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hi,

Does someone know whether it is a bad practice to create exception
objects through reflection and throw them? I want to use this in order
to dynamically convert from an integer errorCode to an exception (via a
map errorCode -> typeof( Exception))?

Assembly excAssembly = _exceptionType.Assembly;
Object [] args = new Object [ 1] { _errorCode };
Exception e =
(Exception)excAssembly.CreateInstance( _exceptionType.FullName,

false,

BindingFlags.CreateInstance,

null,

args,

null,

null);
throw (e);

Thanks in advance,
Kaloian.

Dec 30 '05 #2
That's very bad code practice.
Exceptions are too expensive to use for processing, don't do that.

If your entire objective is to raise an Exception, then how can it be bad
practice?

What are you up to?
"ka******@gmail.com" wrote:
Hi,

Does someone know whether it is a bad practice to create exception
objects through reflection and throw them? I want to use this in order
to dynamically convert from an integer errorCode to an exception (via a
map errorCode -> typeof( Exception))?

Assembly excAssembly = _exceptionType.Assembly;
Object [] args = new Object [ 1] { _errorCode };
Exception e =
(Exception)excAssembly.CreateInstance( _exceptionType.FullName,

false,

BindingFlags.CreateInstance,

null,

args,

null,

null);
throw (e);

Thanks in advance,
Kaloian.

Dec 30 '05 #3
John <Jo**@discussions.microsoft.com> wrote:
That's very bad code practice.
Exceptions are too expensive to use for processing, don't do that.


What exactly do you mean by "for processing"? Whether exceptions are
too expensive depends on how often the relevant situation will occur,
and what the performance demands are.

Just how expensive do you believe exceptions are?

Without trying it (or following the link yet) how many exceptions do
you think can be thrown in a second? You may well be surprised.

See http://www.pobox.com/~skeet/csharp/exceptions.html for my take on
the issue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 30 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by mihai | last post: by
40 posts views Thread by Kevin Yu | last post: by
11 posts views Thread by sternr | last post: by
40 posts views Thread by Sek | last post: by
2 posts views Thread by Anthony Biondo Jr | last post: by
1 post views Thread by usenet | last post: by
14 posts views Thread by Jonathan Wood | last post: by
4 posts views Thread by =?Utf-8?B?cmJEZXZlbG9wZXI=?= | 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.