473,396 Members | 2,024 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 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 1807
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Oueis | last post by:
I've been struggling with this question for a while. What is better design? To design functions to return error codes when an error occures, or to have them throw exceptions. If you chose the...
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...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
11
by: sternr | last post by:
Hey, I use the following code: try { //Some code that has an exception } catch (Exception ex) { if (ex.innerException!=null) {throw ex.innerException;} }
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...
2
by: Anthony Biondo Jr | last post by:
I was wondering how to handle an error in a web service. If our web service encounters a connection error or any other error what is the best practice for returning an error? Do you return a...
1
by: usenet | last post by:
I wrote some sample code (see below) for nested exception throwing i.e. my catch blocks are throwing exceptions of their own (for simplicity I used standard exceptions). I am getting some...
14
by: Jonathan Wood | last post by:
In code that I have enclosed in a try block, it's fairly common for me to respond to error conditions with something like this: throw new Exception("Something went horribly wrong!"); But I've...
4
by: =?Utf-8?B?cmJEZXZlbG9wZXI=?= | last post by:
In the code below, I'm setting a watch on the variable dbCon. When I expand the watch on dbCon, one of the visible items under dbCon is "ServerVersion." When the dbCon.Close() executes, I see the...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.