473,785 Members | 3,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you guys handle error's from class libraries?

Hi all,

I'm currently writing a load of class libraries, but not the main
application iteslf.

I want to provide some method for reporting errors back to the main
application.

At the moment I have a bool errBl and a string errMsg in my classes.
When errBl is ture, one could see what the error was in errMsg.

It works ok, but its quite clunky.

How do you guys do it? Trigger an event or something like that?

Thank you,
Andre

Nov 17 '05
17 7613
> is a much better object-oritented (and cleaner) approach. But remember to
place the most specific (specialized) exceptions in the first catch
blocks...if you place Exception first, Application and Argument -exception
won't be caught(!!)


hmm..bad english...Appli cation and Argument exceptions will of course be
caught, but not as their specific exceptions...th ey will be caught as
Exception only. The reason for this is because ApplicationArgu ment,
ArgumentExcepti on and all other specialized exceptions inherits from the
Exception class and therefore will be trapped by the Exception catch-block
which is executed first...
Nov 17 '05 #11
Frode <Fr***@discussi ons.microsoft.c om> wrote:
is a much better object-oritented (and cleaner) approach. But remember to
place the most specific (specialized) exceptions in the first catch
blocks...if you place Exception first, Application and Argument -exception
won't be caught(!!)


hmm..bad english...Appli cation and Argument exceptions will of course be
caught, but not as their specific exceptions...th ey will be caught as
Exception only. The reason for this is because ApplicationArgu ment,
ArgumentExcepti on and all other specialized exceptions inherits from the
Exception class and therefore will be trapped by the Exception catch-block
which is executed first...


Actually, that's not true - it will fail to compile, with an error such
as:

Test.cs(16,16): error CS0160: A previous catch clause already catches
all exceptions of this or a super type ('System.Except ion')

--
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 #12
Jon,

You are absolutly right. I was jumping to fast to the conclusion without
checking - my mistake.
--
-------------
Frode
"Jon Skeet [C# MVP]" wrote:
Frode <Fr***@discussi ons.microsoft.c om> wrote:
is a much better object-oritented (and cleaner) approach. But remember to
place the most specific (specialized) exceptions in the first catch
blocks...if you place Exception first, Application and Argument -exception
won't be caught(!!)


hmm..bad english...Appli cation and Argument exceptions will of course be
caught, but not as their specific exceptions...th ey will be caught as
Exception only. The reason for this is because ApplicationArgu ment,
ArgumentExcepti on and all other specialized exceptions inherits from the
Exception class and therefore will be trapped by the Exception catch-block
which is executed first...


Actually, that's not true - it will fail to compile, with an error such
as:

Test.cs(16,16): error CS0160: A previous catch clause already catches
all exceptions of this or a super type ('System.Except ion')

--
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 #13
Hi Ignacio,

Yeah I'm doing that at the moment.

Thanks for the post,
Andre

Nov 17 '05 #14
Hi Chris,

Thanks for the little InnerException tip.

Many thanks,
Andre

Nov 17 '05 #15
Also,

Would it not be a good design to provide a way for the client to
"plug-in" some functionality into my library (and in this case for
error handling)?

So when a client instantiates my class, I'll provide the option for the
client to specify an error handling method?

Ex: First I show how the client will use the Library, then I show some
public delegate, and finally the implementation of the Library class

//Client
//Error function
public void DisplayError(st ring message)
{....}

//use of Library
ShowError myShowError = new ShowError(Displ ayError);

Library myLibrary = new Library(myShowE rror);
//Some public delegate
public delegate void ShowError(strin g message);

//Library
//Member
ShowError _showError;

//Constructor
public Library()
{..}

public Library(ShowErr or showError)
{ _showError = showError; }

public TriggerError()
{
if(_showError != null)
_showError("Dis play this error to client");
else
throw new Exception...
}

Is this a viable solution?

Is it bad practice to have the error handling in the library rather
than at the client. I'm just thinking that it'll be a real hasstle to
do a try{} catch on the client for frequently used Libraries.

I'd appreciate any comments regarding the design.

Nov 17 '05 #16
<ah****@gmail.c om> wrote:
Would it not be a good design to provide a way for the client to
"plug-in" some functionality into my library (and in this case for
error handling)?
The furthest I'd go with that is to allow the client to specify a
logger - and that could be used for diagnostics as well as errors. I
wouldn't try to *handle* the error though.
Is it bad practice to have the error handling in the library rather
than at the client.
Yes, unless the library can truly handle the error and still fulfil its
contract.
I'm just thinking that it'll be a real hasstle to
do a try{} catch on the client for frequently used Libraries.


The client shouldn't be putting try/catch all over the place, because
exceptions shouldn't be generated in the normal success case - usually
you put a try/catch against specific exceptions that you know you can
handle and continue from, and at the top level of the application to
report truly unexpected and unrecoverable (per unit of work) errors to
the user.

--
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 #17
Thant makes sense. Thanks Jon.

Jon wrote:
<ah****@gmail.c om> wrote:
Would it not be a good design to provide a way for the client to
"plug-in" some functionality into my library (and in this case for
error handling)?


The furthest I'd go with that is to allow the client to specify a
logger - and that could be used for diagnostics as well as errors. I
wouldn't try to *handle* the error though.
Is it bad practice to have the error handling in the library rather
than at the client.


Yes, unless the library can truly handle the error and still fulfil its
contract.
I'm just thinking that it'll be a real hasstle to
do a try{} catch on the client for frequently used Libraries.


The client shouldn't be putting try/catch all over the place, because
exceptions shouldn't be generated in the normal success case - usually
you put a try/catch against specific exceptions that you know you can
handle and continue from, and at the top level of the application to
report truly unexpected and unrecoverable (per unit of work) errors to
the user.

--
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 #18

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

Similar topics

1
3179
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
7
3256
by: Tony Johansson | last post by:
Hello!! Assume I have a handle body pattern with classes called Handle and Body. In the Body class I store one int value for example 7 or some other integer value. In the Handle class I have a pointer to the Body class. If a want to create a STL container of List with the following declaration List <Handle <Body> > list
14
2734
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using push_back, the object I had in hand was getting copied (twice, in fact). That led to a problem, in that my object contained a "handle" to another object, and when the object being pushed went out of scope and was destroyed, the referenced object was...
4
2474
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called themselves, or should i return a code to main and handle the error there? If i don't return them to main, except for the structure, what use is the main function except for calling functions?
6
2874
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in the first seconds the communication is ok, later i receive read/write error. I?ve been in MSDN site and there i discover that the read/write error is a INVALID_HANDLE problem. But why??? I just create the serial communication file and use it....
2
596
by: Jonathan Boivin | last post by:
Hi people, Let me introduce to how I get this error. I have a form which load all my bills representation depending upon filters which each bill is a usercontrol of my own having some textboxes containing bill's datas. (I already replaced the labels by painting the text instead.) When I click on my filter button, it clears the current usercontrol bills and load the new ones upon the current chosen filters. The bug happens when
2
35623
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
3
3154
by: Peterwkc | last post by:
Hello all C++ expert programmer, I have a handle class which point to another class and use the pointer as object. I follow the code from C++ articles submited by someone in this forum. Unfortunately, my compilation is failed. Below is my code : // Main.cpp
6
38067
by: muby | last post by:
Hi everybody :) I'm modifying a C++ code in VC++ 2005 my code snippet void BandwidthAllocationScheduler::insert( Message* msg, BOOL* QueueIsFull,
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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
9947
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
7496
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
6737
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.