473,414 Members | 1,911 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,414 software developers and data experts.

when to catch an exception...


In a somewhat complex application, I've developed plug-in architecture and
am having a problem as to when to catch general exceptions for logging
purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes handling
some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in a
call stack, then catch it and log the details, or should I do it lower down
(in the proxy for example) so that the highest level has no catch statements
because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling would be
much appreciated.

Thanks,

Daniel.
Nov 16 '05 #1
7 1779
Dan,

This is widely debated topic -- I have seen both sides. The one thing I am
following now is, catch an exception in the layer that has a public
interface -- in Windows app this is the UI layer, in a web service it is the
web method, and in case of a web app it is the ASP.NET page. If you plan to
treat an exception as a non-exception, you can still do this in other layers
e.g. you trap an exception and reset the data accordingly, and not let the
outside world know about it.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture and
am having a problem as to when to catch general exceptions for logging
purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes handling some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in a
call stack, then catch it and log the details, or should I do it lower down (in the proxy for example) so that the highest level has no catch statements because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling would be much appreciated.

Thanks,

Daniel.

Nov 16 '05 #2
Dan,

Personally, I prefer the former statement, where you let the exception
bubble to the top most point it can, and catch it there. Exceptions are
meant as an indicator when something happens which generally should not have
happened (for one reason or another). This does mean that one has to be
more diligent though in their code. For example, if trying to access a
file, you should never have an exception thrown because it doesn't exist,
because you should be checking for the existence of the file before you open
it.

However, with a plug in architecture, I would say that you would want to
wrap the calls to the plug-ins in try/catch blocks, because there is a very
defined boundary there between what is in your control, and what is out of
your control. As a matter of fact, I think this is probably a good
guideline in general (it works for me =) ).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture and
am having a problem as to when to catch general exceptions for logging
purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes
handling some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in a
call stack, then catch it and log the details, or should I do it lower
down (in the proxy for example) so that the highest level has no catch
statements because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling would
be much appreciated.

Thanks,

Daniel.

Nov 16 '05 #3
Nicholas,

Thanks for your reply.

In the case of a windows service, where a file doesn't exist as a part of
some configuration within a plugin for example, then isn't the best way to
pass this information back to the core through an exception? More so given
the code cannot continue past this point, and no alternatives exist...

Because it's a silent application in terms of user feedback, I'm struggling
a little to see the line between good old error checking and exceptions. If
there's something wrong, then I want a logger I've got to report this. It
seems the most logical way to do this is to have the catches setup at a high
level logging the error.

I think the main difference is the line is moving as to what contributes to
a fatal error. If a user's filling out a form for example, the error
checking insures they've entered everything correctly before proceeding. If
configuration on the service is incorrect, then it cannot run until
someone's fixed the configuration, and restarted the service, meaning that
all assumption inconsistencies become fatal.

Thanks.

Daniel.
Personally, I prefer the former statement, where you let the exception
bubble to the top most point it can, and catch it there. Exceptions are
meant as an indicator when something happens which generally should not
have happened (for one reason or another). This does mean that one has to
be more diligent though in their code. For example, if trying to access a
file, you should never have an exception thrown because it doesn't exist,
because you should be checking for the existence of the file before you
open it.

However, with a plug in architecture, I would say that you would want
to wrap the calls to the plug-ins in try/catch blocks, because there is a
very defined boundary there between what is in your control, and what is
out of your control. As a matter of fact, I think this is probably a good
guideline in general (it works for me =) ).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture
and am having a problem as to when to catch general exceptions for
logging purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes
handling some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in a
call stack, then catch it and log the details, or should I do it lower
down (in the proxy for example) so that the highest level has no catch
statements because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling would
be much appreciated.

Thanks,

Daniel.


Nov 16 '05 #4
Dan,

In the case of a service, what I said before still stands. With a plug
in architecture, you have two boundaries. The first is the boundary between
the service and the SCM (rather your code and the runtime, and then the
SCM), and your code and the plug ins.

For a service, when you can't run because something is not configured,
you will see immediately that there is an error because the service won't
start.

However, with the plug ins, I don't think that if there is a failure in
the plug in, that constitutes a fatal error. The assumption here is that
you have more than one plug in, so failure of one shouldn't mean failure of
all of them. If one of them fails (which you detect in your exception
handling around the calls to the plug in) then you should remove it from the
list of valid plug ins, and log that event somewhere (that there was a
failure, and the plug in was removed). You then go on to run the rest of
the plug ins.

If you have only one plug in, then you have a justification for the
architecture to begin with (you do, right?), and see a need for it. Just
because if one fails doesn't mean the service should shut down, just rather,
that all of the plug ins failed. The service should continue running. Just
log the error, and continue.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:u4**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

Thanks for your reply.

In the case of a windows service, where a file doesn't exist as a part of
some configuration within a plugin for example, then isn't the best way to
pass this information back to the core through an exception? More so given
the code cannot continue past this point, and no alternatives exist...

Because it's a silent application in terms of user feedback, I'm
struggling a little to see the line between good old error checking and
exceptions. If there's something wrong, then I want a logger I've got to
report this. It seems the most logical way to do this is to have the
catches setup at a high level logging the error.

I think the main difference is the line is moving as to what contributes
to a fatal error. If a user's filling out a form for example, the error
checking insures they've entered everything correctly before proceeding.
If configuration on the service is incorrect, then it cannot run until
someone's fixed the configuration, and restarted the service, meaning that
all assumption inconsistencies become fatal.

Thanks.

Daniel.
Personally, I prefer the former statement, where you let the exception
bubble to the top most point it can, and catch it there. Exceptions are
meant as an indicator when something happens which generally should not
have happened (for one reason or another). This does mean that one has
to be more diligent though in their code. For example, if trying to
access a file, you should never have an exception thrown because it
doesn't exist, because you should be checking for the existence of the
file before you open it.

However, with a plug in architecture, I would say that you would want
to wrap the calls to the plug-ins in try/catch blocks, because there is a
very defined boundary there between what is in your control, and what is
out of your control. As a matter of fact, I think this is probably a
good guideline in general (it works for me =) ).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture
and am having a problem as to when to catch general exceptions for
logging purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes
handling some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in a
call stack, then catch it and log the details, or should I do it lower
down (in the proxy for example) so that the highest level has no catch
statements because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling
would be much appreciated.

Thanks,

Daniel.



Nov 16 '05 #5
Nicholas,

Thanks, you've helped make things a lot clearer. I need to clean up my
terminology too... ;o)

Daniel.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu****************@TK2MSFTNGP11.phx.gbl...
Dan,

In the case of a service, what I said before still stands. With a plug
in architecture, you have two boundaries. The first is the boundary
between the service and the SCM (rather your code and the runtime, and
then the SCM), and your code and the plug ins.

For a service, when you can't run because something is not configured,
you will see immediately that there is an error because the service won't
start.

However, with the plug ins, I don't think that if there is a failure in
the plug in, that constitutes a fatal error. The assumption here is that
you have more than one plug in, so failure of one shouldn't mean failure
of all of them. If one of them fails (which you detect in your exception
handling around the calls to the plug in) then you should remove it from
the list of valid plug ins, and log that event somewhere (that there was a
failure, and the plug in was removed). You then go on to run the rest of
the plug ins.

If you have only one plug in, then you have a justification for the
architecture to begin with (you do, right?), and see a need for it. Just
because if one fails doesn't mean the service should shut down, just
rather, that all of the plug ins failed. The service should continue
running. Just log the error, and continue.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:u4**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

Thanks for your reply.

In the case of a windows service, where a file doesn't exist as a part of
some configuration within a plugin for example, then isn't the best way
to pass this information back to the core through an exception? More so
given the code cannot continue past this point, and no alternatives
exist...

Because it's a silent application in terms of user feedback, I'm
struggling a little to see the line between good old error checking and
exceptions. If there's something wrong, then I want a logger I've got to
report this. It seems the most logical way to do this is to have the
catches setup at a high level logging the error.

I think the main difference is the line is moving as to what contributes
to a fatal error. If a user's filling out a form for example, the error
checking insures they've entered everything correctly before proceeding.
If configuration on the service is incorrect, then it cannot run until
someone's fixed the configuration, and restarted the service, meaning
that all assumption inconsistencies become fatal.

Thanks.

Daniel.
Personally, I prefer the former statement, where you let the
exception bubble to the top most point it can, and catch it there.
Exceptions are meant as an indicator when something happens which
generally should not have happened (for one reason or another). This
does mean that one has to be more diligent though in their code. For
example, if trying to access a file, you should never have an exception
thrown because it doesn't exist, because you should be checking for the
existence of the file before you open it.

However, with a plug in architecture, I would say that you would want
to wrap the calls to the plug-ins in try/catch blocks, because there is
a very defined boundary there between what is in your control, and what
is out of your control. As a matter of fact, I think this is probably a
good guideline in general (it works for me =) ).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture
and am having a problem as to when to catch general exceptions for
logging purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes
handling some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in
a call stack, then catch it and log the details, or should I do it
lower down (in the proxy for example) so that the highest level has no
catch statements because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling
would be much appreciated.

Thanks,

Daniel.



Nov 16 '05 #6
Thanks for your reply and help. =o)
"Manohar Kamath" <mk*****@TAKETHISOUTkamath.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Dan,

This is widely debated topic -- I have seen both sides. The one thing I am
following now is, catch an exception in the layer that has a public
interface -- in Windows app this is the UI layer, in a web service it is
the
web method, and in case of a web app it is the ASP.NET page. If you plan
to
treat an exception as a non-exception, you can still do this in other
layers
e.g. you trap an exception and reset the data accordingly, and not let the
outside world know about it.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture
and
am having a problem as to when to catch general exceptions for logging
purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes

handling
some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in a
call stack, then catch it and log the details, or should I do it lower

down
(in the proxy for example) so that the highest level has no catch

statements
because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling would

be
much appreciated.

Thanks,

Daniel.


Nov 16 '05 #7
Nicholas,

I've been working along these lines in modifying the application and have
come up with this...

One use of the plug-in's is to send and receive data from various sources.
One of these happens to be using database connections. In this case, should
I catch the exception at this level, and call "Close" on the connection
before rethrowing the exception after "tidying" up?

If I don't and there's a timeout, or stored procedure error say, does this
close the connection to the database?
If it doesn't what happens when I try to create a new connection to the same
database again? Presumably this will utilise the connection pool and reuse
the connection I had before. Would a "new" on this connection close the old
one before creating the new one?

I've got the code below... Most of the project is in C#, but some is in VB,
this happens to be the latter...

Public Overrides Sub CompleteReceive()

' open the database

Dim dbData As OdbcConnection = New OdbcConnection
dbData.ConnectionString = connectionString
dbData.Open()
' call the header update to set the record to "processed"

Dim dbcommand As OdbcCommand = New OdbcCommand
dbcommand.Connection = dbData
dbcommand.CommandText = "{Call " & inHeaderUpdateproc & " (?, ?,
?)}"
dbcommand.CommandType = CommandType.StoredProcedure
dbcommand.Parameters.Add("@MsgNumber",
NullToBlank(currentMsgNumber))
dbcommand.Parameters.Add("@Status", "PROCESSED")
dbcommand.Parameters.Add("@Size", -1)
dbcommand.ExecuteNonQuery()

dbData.Close()

End Sub
Thanks again for your feedback and help on this.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uu****************@TK2MSFTNGP11.phx.gbl...
Dan,

In the case of a service, what I said before still stands. With a plug
in architecture, you have two boundaries. The first is the boundary
between the service and the SCM (rather your code and the runtime, and
then the SCM), and your code and the plug ins.

For a service, when you can't run because something is not configured,
you will see immediately that there is an error because the service won't
start.

However, with the plug ins, I don't think that if there is a failure in
the plug in, that constitutes a fatal error. The assumption here is that
you have more than one plug in, so failure of one shouldn't mean failure
of all of them. If one of them fails (which you detect in your exception
handling around the calls to the plug in) then you should remove it from
the list of valid plug ins, and log that event somewhere (that there was a
failure, and the plug in was removed). You then go on to run the rest of
the plug ins.

If you have only one plug in, then you have a justification for the
architecture to begin with (you do, right?), and see a need for it. Just
because if one fails doesn't mean the service should shut down, just
rather, that all of the plug ins failed. The service should continue
running. Just log the error, and continue.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:u4**************@TK2MSFTNGP10.phx.gbl...
Nicholas,

Thanks for your reply.

In the case of a windows service, where a file doesn't exist as a part of
some configuration within a plugin for example, then isn't the best way
to pass this information back to the core through an exception? More so
given the code cannot continue past this point, and no alternatives
exist...

Because it's a silent application in terms of user feedback, I'm
struggling a little to see the line between good old error checking and
exceptions. If there's something wrong, then I want a logger I've got to
report this. It seems the most logical way to do this is to have the
catches setup at a high level logging the error.

I think the main difference is the line is moving as to what contributes
to a fatal error. If a user's filling out a form for example, the error
checking insures they've entered everything correctly before proceeding.
If configuration on the service is incorrect, then it cannot run until
someone's fixed the configuration, and restarted the service, meaning
that all assumption inconsistencies become fatal.

Thanks.

Daniel.
Personally, I prefer the former statement, where you let the
exception bubble to the top most point it can, and catch it there.
Exceptions are meant as an indicator when something happens which
generally should not have happened (for one reason or another). This
does mean that one has to be more diligent though in their code. For
example, if trying to access a file, you should never have an exception
thrown because it doesn't exist, because you should be checking for the
existence of the file before you open it.

However, with a plug in architecture, I would say that you would want
to wrap the calls to the plug-ins in try/catch blocks, because there is
a very defined boundary there between what is in your control, and what
is out of your control. As a matter of fact, I think this is probably a
good guideline in general (it works for me =) ).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message news:OV**************@TK2MSFTNGP11.phx.gbl...

In a somewhat complex application, I've developed plug-in architecture
and am having a problem as to when to catch general exceptions for
logging purposes.

In each plug-in class library, for example, I'm assuming it's throw on
errors, and that the core then catches them. I've got proxy classes
handling some fiddly details of the plug-ins.

Should I allow the exception to bubble to the top most point it can in
a call stack, then catch it and log the details, or should I do it
lower down (in the proxy for example) so that the highest level has no
catch statements because all the methods it calls are handle them?

Any suggestions or documentation on this sort of exception handling
would be much appreciated.

Thanks,

Daniel.



Nov 16 '05 #8

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

Similar topics

5
by: Bernard Koninckx | last post by:
Hi everybody, The following code (putted in a inherited object from AbstractTableModel object) make some errors : public void deleteRow(int rowToDelete){ try{ Object dataObject =...
9
by: Allan Ebdrup | last post by:
Hi I'm having a discussion with another programmer about a best practice for thrownig exceptions. I want to create a rich class library with many different exception classes in their own...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
2
by: Gummy | last post by:
Hello All, I have a webpage that has two dropdown listboxes. Based on what is selected in these dropdown listboxes, it filters a DataGrid . That works fine. In the DataGrid , when I go to edit...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
2
by: CodeSlayer | last post by:
Hi all, This one really has me and the other .Net developers at my work stumped. I have an application that is doing the following: 1 - attempt to validate that user can create a windows task...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
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: 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...
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
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,...
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
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...
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,...
0
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...
0
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...

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.