473,396 Members | 1,767 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.

How to handle exceptions in a library dll?

I am writing a small library in C#, which will be compiled to dll and
shared by multiple applications.

This is sorta new to me. I am wondering how professionals handle
exceptions in a dll library.
Should we simply catch it, re-throw it, and leave the handling to the
consumer class? This what I have been doing, but not sure if this is
considered a standard way of doing it.

Thank you if you could give me little hint.

Jul 25 '08 #1
6 6945
On Thu, 24 Jul 2008 19:48:03 -0700, Author <gn********@gmail.comwrote:
I am writing a small library in C#, which will be compiled to dll and
shared by multiple applications.

This is sorta new to me. I am wondering how professionals handle
exceptions in a dll library.
Should we simply catch it, re-throw it, and leave the handling to the
consumer class? This what I have been doing, but not sure if this is
considered a standard way of doing it.
Ideally, you would only throw exceptions related to the caller's input.
And you would do so at the point of checking the input, rather than
waiting for bad input to cause an exception later.

In some cases, you won't know the caller's input is bad until you try to
use it. In those cases, there may be some value in catching the
exception, using it as the inner exception for a new exception that is
descriptive and useful to the caller and throwing that new exception.

Other than that, I think there's not much point in catching and rethrowing
exceptions. If you don't have something useful to do with the exception,
don't catch it at all. Just let it go all the way back to the caller
instead.

Pete
Jul 25 '08 #2
On Jul 25, 7:54*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 24 Jul 2008 19:48:03 -0700, Author <gnewsgr...@gmail.comwrote:
I am writing a small library in C#, which will be compiled to dll and
shared by multiple applications.
This is sorta new to me. *I am wondering how professionals handle
exceptions in a dll library.
Should we simply catch it, re-throw it, and leave the handling to the
consumer class? *This what I have been doing, but not sure if this is
considered a standard way of doing it.

Ideally, you would only throw exceptions related to the caller's input. *
And you would do so at the point of checking the input, rather than *
waiting for bad input to cause an exception later.

In some cases, you won't know the caller's input is bad until you try to *
use it. *In those cases, there may be some value in catching the *
exception, using it as the inner exception for a new exception that is *
descriptive and useful to the caller and throwing that new exception.

Other than that, I think there's not much point in catching and rethrowing *
exceptions. *If you don't have something useful to do with the exception, *
don't catch it at all. *Just let it go all the way back to the caller *
instead.

Pete
Pete approach is excellent.

-Cnu
Jul 25 '08 #3
On Jul 24, 10:54*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Thu, 24 Jul 2008 19:48:03 -0700, Author <gnewsgr...@gmail.comwrote:
I am writing a small library in C#, which will be compiled to dll and
shared by multiple applications.
This is sorta new to me. *I am wondering how professionals handle
exceptions in a dll library.
Should we simply catch it, re-throw it, and leave the handling to the
consumer class? *This what I have been doing, but not sure if this is
considered a standard way of doing it.

Ideally, you would only throw exceptions related to the caller's input. *
And you would do so at the point of checking the input, rather than *
waiting for bad input to cause an exception later.

In some cases, you won't know the caller's input is bad until you try to *
use it. *In those cases, there may be some value in catching the *
exception, using it as the inner exception for a new exception that is *
descriptive and useful to the caller and throwing that new exception.

Other than that, I think there's not much point in catching and rethrowing *
exceptions. *If you don't have something useful to do with the exception, *
don't catch it at all. *Just let it go all the way back to the caller *
instead.

Pete
Great. Seems my intuition is correct. In one class of my library, I
need to return a bool. I set the bool variable to false in the catch
block and then throw the exception. I think this exception re-throw
makes sense, since I need to reset the value of the bool variable
before I return it.
Jul 25 '08 #4
On Jul 25, 2:14*pm, Author <gnewsgr...@gmail.comwrote:
Great. Seems my intuition is correct. *In one class of my library, I
need to return a bool. *I set the bool variable to false in the catch
block and then throw the exception. *I think this exception re-throw
makes sense, since I need to reset the value of the bool variable
before I return it.
If you're rethrowing the exception, you're not returning the value
anyway though.

Jon
Jul 25 '08 #5
On Jul 25, 9:20*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 25, 2:14*pm, Author <gnewsgr...@gmail.comwrote:
Great. Seems my intuition is correct. *In one class of my library, I
need to return a bool. *I set the bool variable to false in the catch
block and then throw the exception. *I think this exception re-throw
makes sense, since I need to reset the value of the bool variable
before I return it.

If you're rethrowing the exception, you're not returning the value
anyway though.

Jon
Yes, that's correct. I didn't realize it.
Jul 25 '08 #6
On Jul 25, 10:55*am, Author <gnewsgr...@gmail.comwrote:
On Jul 25, 9:20*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 25, 2:14*pm, Author <gnewsgr...@gmail.comwrote:
Great. Seems my intuition is correct. *In one class of my library, I
need to return a bool. *I set the bool variable to false in the catch
block and then throw the exception. *I think this exception re-throw
makes sense, since I need to reset the value of the bool variable
before I return it.
If you're rethrowing the exception, you're not returning the value
anyway though.
Jon

Yes, that's correct. *I didn't realize it.
I will dare to express slightly different opinion.
It's proven very valuable to me to always catch and rethrow the
exception, adding as much information (without compromising
application security) as possible.

I absolutely HATE to see an error (happens a lot w/ microsoft's
products of all kinds) saying "Something bad happened". Why not say
what exactly, where, what were the values of the incoming parameters,
intermediate values, even how far down the execution path it happened?
That info is usually not available in the original exception (or inner
exception).

Yes, if you are able to debug, you can step through and see all that.
Adding it to the re-thrown exception only may speed up your
development a bit, but the real value comes at run time.

Then let the client decide how much of the info and how to use it.

I hope I am not too much off and only trying to bring another
perspective

Regards:
G.S.
Jul 25 '08 #7

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

Similar topics

5
by: Asfand Yar Qazi | last post by:
Hi, Just wondering, if I do a: std::set_new_handler(std::terminate) I won't have to worry about a std::bad_alloc being thrown when I do a 'new ...' or a 'new(nothrow) ...', right? Its...
6
by: DraguVaso | last post by:
Hi, In my application, on some given actions while debugging in Visual Studio, I suddenly get a "System.ComponentModel.Win32Exception was unhandled" Message="Error creating window handle."...
10
by: Cool Guy | last post by:
Consider: void Start() { if (!TryToDoSomething()) ShowErrorMessage(); }
17
by: ahaupt | last post by:
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...
4
by: Boni | last post by:
Dear all, is it possible to handle 2 exception tipes in one block? I.e Try if XXX then
3
by: chris | last post by:
hello, I can't seem to make this work: VS2005 I have a simple program that uses a backgroundworker control to execute a long process (webservice call) if that webservice call fails, i want to...
3
by: clintonb | last post by:
Some programmers, and even Microsoft documents, say you should only throw exceptions for exceptional situations. So how are others handling all the other unexceptional errors? How are you...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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,...

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.