473,563 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1789
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******** ******@TK2MSFTN GP11.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:OV******** ******@TK2MSFTN GP11.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:OV******** ******@TK2MSFTN GP11.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:u4******** ******@TK2MSFTN GP10.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message news:OV******** ******@TK2MSFTN GP11.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.c om> wrote in
message news:uu******** ********@TK2MSF TNGP11.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:u4******** ******@TK2MSFTN GP10.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message news:OV******** ******@TK2MSFTN GP11.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*****@TAKETH ISOUTkamath.com > wrote in message
news:%2******** ********@TK2MSF TNGP10.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******** ******@TK2MSFTN GP11.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.Connecti onString = connectionStrin g
dbData.Open()
' call the header update to set the record to "processed"

Dim dbcommand As OdbcCommand = New OdbcCommand
dbcommand.Conne ction = dbData
dbcommand.Comma ndText = "{Call " & inHeaderUpdatep roc & " (?, ?,
?)}"
dbcommand.Comma ndType = CommandType.Sto redProcedure
dbcommand.Param eters.Add("@Msg Number",
NullToBlank(cur rentMsgNumber))
dbcommand.Param eters.Add("@Sta tus", "PROCESSED" )
dbcommand.Param eters.Add("@Siz e", -1)
dbcommand.Execu teNonQuery()

dbData.Close()

End Sub
Thanks again for your feedback and help on this.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:uu******** ********@TK2MSF TNGP11.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:u4******** ******@TK2MSFTN GP10.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.co m

"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
message news:OV******** ******@TK2MSFTN GP11.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
5500
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 = datas.get(rowToDelete); String idName = idField.getColumnName(); Method method = dataObjectClass.getMethod("get" +
9
8484
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 namespace, so they are easy to find with intellisense. He wants to just throw Exception or one of the build in exceptions. Do you have some good arguments...
4
2029
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 like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
2
2063
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 a row, I change the textbox (or other control), click Update, but it doesn't save my data. I then did this... In Page_Load, I added code to...
8
2242
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 exception should be thrown only in exceptional situations. It turned out that each of my colleagues had their own interpretation about what an...
2
1910
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 via COM interops 2 - an exception is thrown because user doesn't exist 3 - Exception is caught by calling code shown below:
32
6100
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 understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
9
1922
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 months in Java, I am thinking why Win32 APIs or even .Net documentation not clear on which methods will throw exception or what exceptions can be...
0
7583
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...
0
7888
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. ...
0
8106
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...
0
6255
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...
1
5484
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...
0
5213
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
924
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...

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.