473,233 Members | 1,454 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,233 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 1802
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.