473,725 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looking for advice on this rethrow of exception

I'm just looking for some other opinions on this.

I have a control that does employee lookups, auto complete of the name
etc.

Let say that this control is:
Company.Control s.Web.LookupCon trols.EmployeeL ookupControl

Now I have a Employee class:
Company.Busines s.Employee

The employee class takes an employee ID as one of its constructors, so
if you pass it an ID number for an employee that doesn't exist (for
example -1 or 100000000), it throws an exception:
Company.Busines s.EmployeeNotFo undException

Now my dilema is what to do when my application passes an invalid ID
number to my control EmployeeLookupC ontrol. The lookup control catches
the EmployeeNotFoun dException.

One part of me thinks that it should simply rethrown the
EmployeeNotFoun dException, and part of me thinks it should thrown it's
OWN exception so that consumers of the LookupControl would only have to
expect exceptions that belong to it, not other DLLs/namespaces. So
then I started thinking, what would I name the new exception if I did
choose to throw an exception? It seems like EmployeeNotFoun dException
properly describes the problem, but I am not sure having 2 exceptions
with the same name is a good idea, even if they are in different
namespaces.

So to boil it down.
Option 1: Rethrow the Company.Busines s.EmployeeNotFo undException
exception. Then applications would have to have a reference to the
DLL that holds that exception class, and developers would have to deal
with exceptions from other DLLs than the one they are dealing with when
setting the ID value of the employee.

Option 2: Make an exception class within the LookupControls DLL and
throw this exception.
Sub Option 2a: Use the same name EmployeeNotFoun dException. It's
ok b/c its in a different namespace.
Sub Option 2b: Make up a different name for the exception b/c it's
not good practice to have 2 exceptions with the same name, even if they
are in different namespaces.

Nov 17 '05 #1
6 1627
"cmay" <cm**@walshgrou p.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
I have a control that does employee lookups, auto complete of the name
etc.
The employee class takes an employee ID as one of its constructors, so
if you pass it an ID number for an employee that doesn't exist (for
example -1 or 100000000), it throws an exception:
Company.Busines s.EmployeeNotFo undException
As a general rule, it's simply not cricket (excuse analogy - we've just won
the ashes) to have constructors throw exceptions. A better idea would be a
static factory method called FromID or something similar.
Now my dilema is what to do when my application passes an invalid ID
number to my control EmployeeLookupC ontrol. The lookup control catches
the EmployeeNotFoun dException.


Why not throw an ArgumentExcepti on and pass in the EmployeeNotFoun dException
as the inner exception?

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2
I would choose option 1... But I'm not an expert programmer...
Why deal with exception from other dll is a problem?

Nov 17 '05 #3
cmay wrote:
So to boil it down.
Option 1: Rethrow the Company.Busines s.EmployeeNotFo undException
exception. Then applications would have to have a reference to the
DLL that holds that exception class, and developers would have to deal
with exceptions from other DLLs than the one they are dealing with when
setting the ID value of the employee.
I would let the exception out through, afterall the caller is much more
likely to have some code specific code to be run for not-found-employees
than for some ArgumentExcepti on or what-not.

The applications would *only* need to have a reference to the DLL with
the exception if they wish to use it as an EmployeeNotFoun dException,
they can catch it as Exception, and log it or whatnot, just fine.

You keep saying "rethrow", why are you even catching the exception?
Option 2: Make an exception class within the LookupControls DLL and
throw this exception.
You are going to have to write a lot of these wrappers, and no matter
how many you write, you can never be certain that the code you call will
not throw one you haven't wrapped.

The alternative is to have a SomethingWentWr ongInMyModuleEx ception, and
attach the original Exception to the .Inner of that. But this requires
anyone who wish to catch only EmployeeNotFoun dException to catch
SomethingWentWr ongInMyModuleEx ception and inspect it's .Inner, not very
nice at all.
Sub Option 2a: Use the same name EmployeeNotFoun dException. It's
ok b/c its in a different namespace.
Sub Option 2b: Make up a different name for the exception b/c it's
not good practice to have 2 exceptions with the same name, even if they
are in different namespaces.


The name is not really important here, although I would prefer them to
have the same name (what are namespaces for :). Focus on getting the
design of throw/catch nailed down.

You can have a look around the NG and/or archives, this is not the first
question of it's kind here.

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4
Tim Haughton <ti*********@gm ail.com> wrote:
I have a control that does employee lookups, auto complete of the name
etc.
The employee class takes an employee ID as one of its constructors, so
if you pass it an ID number for an employee that doesn't exist (for
example -1 or 100000000), it throws an exception:
Company.Busines s.EmployeeNotFo undException


As a general rule, it's simply not cricket (excuse analogy - we've just won
the ashes) to have constructors throw exceptions. A better idea would be a
static factory method called FromID or something similar.


While that may have been true in C++, there's no problem throwing
exceptions in C#. It's all over the framework - what would you expect
new FileStream (null, 0) to do, for instance?
Now my dilema is what to do when my application passes an invalid ID
number to my control EmployeeLookupC ontrol. The lookup control catches
the EmployeeNotFoun dException.


Why not throw an ArgumentExcepti on and pass in the EmployeeNotFoun dException
as the inner exception?


That sounds like a good idea to me.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Not sure.
From a programming standpoint, there would be no problem with it, other

than adding the necessity that the application reference the underlying
DLL that the exception belongs to.

But more from a design standpoint, it felt wrong to have a dll throwing
exceptions from another dll. Maybe not.

Nov 17 '05 #6
Thanks guys.

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
48457
by: Gil | last post by:
In C++, I can rethrow the exception I just caught with the throw statement. Can I do something similar in Java? } catch (Exception ex) { throw; }
5
11020
by: A | last post by:
Hi, I'm having some difficulty understanding the semantics of the rethrow keyword. Consider the following code: int main(){ try{ ... } catch(SomeException &SE){
8
5835
by: Taylor | last post by:
I've run in to code with this pattern: try { // do some potentially bad stuff } catch(System.Exception ex) { throw ex; }
10
6921
by: ahaupt | last post by:
Hi all, Why would one want to rethrow an exception? Doesn't that defeat the purpose? Best, Andre
3
1855
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call a mehtod called ExceptionGenerator that will generate a run-time exception. The exception is caught in GenericGridEvent, but it isn't caught in InitializeComponenet (which doens't suprise me). I can't figure out where to catch an error that may...
24
2373
by: Chameleon | last post by:
Is there a possibility to create memory leak, the code below if I run the line: --------------------------------------------------------- MyClass cl = new MyClass(); --------------------------------------------------------- ??? --------------------------------------------------------- MyClass::MyClass() {
5
2860
by: Steven T. Hatton | last post by:
I haven't thought about rethrowing an exception in a long time, and tried to do it the wrong way. Now I'm curious about /why/ it's wrong. My expectation was that the exception instance would simply be passed up the call stack as the same object. It would therefore behave polymorphically. Is the behavior shown below compiler-specific? I'm using gcc 4.0.2. In terms of what goes on at runtime, why does the `throw e' act as if I threw a...
14
1512
by: Rex | last post by:
Re: Looking for Tips/Writeup on overall approach to Exception Processing Hi All - I am fairly new to C# and am wondering how to best implement (overall) Exception Processing within my (reasonably-sized) C# Windows application. I do have a bunch of somewhat random questions on this and if you can help me with only one or a few, that would still be APPRECIATED. Here are my questions: 1. Is it recommended to put all of the "Main" coding...
8
3399
by: reuce-google | last post by:
Hi folks, I want to store an exception and rethrow it later: CException m_pEc = NULL; // Class variable. try { throw new CMemoryException(); }
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.