473,749 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

throwing exception to application.... .if there's any better way to do it??

i'm writing a class library having following code snippet. i want this class
to report an error (by throwing an Exception) to the application using this
class library. the problem is that within that try block there are several
exceptions that this class itself needs to handle (interrnally). now when
the exception, UserAlreadyRegi stered, is thrown the class' catch block
(within library) catches it and no exception is thrown to the application
further using it.
for this in catch block i have put up another throw instruction if the
exception occured was "UserAlreadyReg istered" in order to pass the exception
to the application.
let me know if there's some better way to do it....and plus what is
"InnerException " supposed to do????

try
{

if (this.IsRegiste red(userInfo.Em ailAddress))
throw new Exception("User AlreadyRegister ed"); // the exception
supposed to be caught by application using this class
// code goes on

}
catch (Exception exp)
{

if (exp.Message==" UserAlreadyRegi stered")
throw exp;
// some exception handler

}
thanks.

Farooq Khan
Nov 15 '05 #1
1 1668
Farooq Khan <fa*********@pk .softechww.com> wrote:
i'm writing a class library having following code snippet. i want this class
to report an error (by throwing an Exception) to the application using this
class library. the problem is that within that try block there are several
exceptions that this class itself needs to handle (interrnally). now when
the exception, UserAlreadyRegi stered, is thrown the class' catch block
(within library) catches it and no exception is thrown to the application
further using it.


Only catch the *types* of exception you really want to handle, and
throw a specific UserAlreadyRegi steredException type. Throwing a bare
Exception is a bad idea - it means you can't easily catch specific
exceptions, as you've found in your own code.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

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

Similar topics

21
7279
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void out_of_mem() {
21
4436
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 can we avoid throwing exceptions? If we already have an abject witch was initialized wit _create_ we will be forced to call create in copy constructor and to throw exceptions from it. Have a nice day,
40
13523
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
9
1510
by: Mark Rae | last post by:
Hi, Can anyone please tell me if there is any difference in throwing an exception with or without parentheses, e.g. try { /* code */
40
36109
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 object. TIA Sek
5
1458
by: GG | last post by:
Any thoughts on designing classes wheather to throw the exception and the consumer has to capture it on try catch or wrap all the methods in a main method This is the wrap logic MainMethod{ bool mainRun = false; try { methods... mainRun = true;
7
5143
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot methods internally. My thought was, the exception is being rethrown and propagated by the non-public methods to the public methods, causing performance overhead (stack winding etc). I do agree that, the purpose of throwing the exception by...
5
3831
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details in the application log. The static method is overloaded, and supports passing exceptions and/or strings just like throwing an exception.The class will also fall back to the standard exception handling if something goes wrong in my class. As an...
21
1716
by: Chris M. Thomasson | last post by:
Is it every appropriate to throw in a dtor? I am thinking about a simple example of a wrapper around a POSIX file... ________________________________________________________________________ class file { FILE* m_handle; public: // ; ~file() /* throw() */ {
0
9568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
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...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6801
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
6079
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
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.