473,396 Members | 2,038 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.

C# Error handling

aaj
Hi all

I have an automated application, that runs in the middle of the night.

If certain 'non system' errors occur (things like malformed files, missing
files etc..), I send an automatic Email and write a record to the database.
This is handled in a class.

When these errors occur, once Emailed and written I want to just end the
App, simple as that.

My inital thought was to End the Application at the end of the error
handling class using Application.Exit or Environment Exit. this means I just
write the code once and thats it, the App stops dead!! But while reading up,
It seems to be bad practice.

Is a better way to set a flag within the Error handling class, work its way
up the chain and then exit in the form code. The only problem is, some of
the error handling is nested quite deeply and passing this back to the top
level form may get a bit messy.

Does anyone have any recommendations for best practices on dealing with this
type of error handling.

many thanks.

Andy



Nov 17 '05 #1
4 2506
You could define a custom exception that is thrown whenever this case
arises. The code at the highest level catches the exception and
performs the procedures to shut down.

Nov 17 '05 #2
You made two statements that are interesting in combination:
I have an automated application, that runs in the middle of the night. /and/ Is a better way to set a flag within the Error handling class, work its
way
up the chain and then exit in the form code.
So this is a windows forms application that runs in the middle of the night.

Not knowing your code, I'm going to _guess_ that you have combined the logic
of the GUI with the business logic that can run "in the middle of the
night".
And this is where your problem lies.

I would suggest that the best option is to take your app and split it. One
side is the GUI and the other side is the "business layer" that has all the
code for doing the fundamental work, but has NO interface code in it.

Then, make a new EXE that is a console application. This EXE will be the
one that runs in the middle of the night, or runs as a service, or runs from
the windows scheduler, or runs on a server, or any of the other automated
needs that are likely to arise. This EXE will start, look at the local
environment, (perhaps get data from a settings file or command line
parameters), and check for errors in the data. If errors are found, you
have no code to unwind. You stop from the caller.

If there are no errors, then you proceed.

This is not a new situation. I've seen it re-enacted repeatedly for years.
The solution is the same, whether you use COM or .Net or even C dlls.
Seperate the business from the interface and use a new interface for the
automated work.

Good Luck,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"aaj" <aa*@aaj.com> wrote in message
news:1117630892.5d1cb1393192df959e430d0640ff53f3@t eranews... Hi all

I have an automated application, that runs in the middle of the night.

If certain 'non system' errors occur (things like malformed files, missing
files etc..), I send an automatic Email and write a record to the
database.
This is handled in a class.

When these errors occur, once Emailed and written I want to just end the
App, simple as that.

My inital thought was to End the Application at the end of the error
handling class using Application.Exit or Environment Exit. this means I
just
write the code once and thats it, the App stops dead!! But while reading
up,
It seems to be bad practice.

Is a better way to set a flag within the Error handling class, work its
way
up the chain and then exit in the form code. The only problem is, some of
the error handling is nested quite deeply and passing this back to the top
level form may get a bit messy.

Does anyone have any recommendations for best practices on dealing with
this
type of error handling.

many thanks.

Andy



Nov 17 '05 #3
aaj
Thanks guys

Nick, your quite correct, to give a bit of background, the GUI was just a
hang on from before the days when the code was automated (it runs as a
scheduled task now, and because of the Email and database logging doesn't
need any GUI, I just couldn't see the wood for the trees 8-)

that said though, both solutions seem to suggest handling the the errors and
passing them back up through the chain and exiting the App in its natural
place....

What I'm trying to ask (but not very well)... is if I have a class that
utilises another class, which inturn utilises another series of classes, and
one of the grandchild classes detects that something is wrong (not a system
fault, but some thing which is semi predictable) is it acceptable (good/bad
practice) to terminate the application right there in the grandchild class
(as there is no need for any more work to be done by the app, nothing has
been written yet, so no rewinding necessary etc..)

or

should the fact that a problem has been detected be passed all the way up
the chain to the control class where the rest of the code can be skipped in
a controlled manner and let the program terminate in its normal way.

Just as a bit of extra info, the reason I don't do all the checks before
proceeding with the importing of the data is purely down to efficiency.
Because the data is correct 98% of the time, I validate the data as it
streams in, rather than testing it and then streaming it in, and if
anomilies that should stop the import occur.

many thanks again, and I hope the above make sense

Andy

"aaj" <aa*@aaj.com> wrote in message
news:1117630892.5d1cb1393192df959e430d0640ff53f3@t eranews...
Hi all

I have an automated application, that runs in the middle of the night.

If certain 'non system' errors occur (things like malformed files, missing
files etc..), I send an automatic Email and write a record to the
database.
This is handled in a class.

When these errors occur, once Emailed and written I want to just end the
App, simple as that.

My inital thought was to End the Application at the end of the error
handling class using Application.Exit or Environment Exit. this means I
just
write the code once and thats it, the App stops dead!! But while reading
up,
It seems to be bad practice.

Is a better way to set a flag within the Error handling class, work its
way
up the chain and then exit in the form code. The only problem is, some of
the error handling is nested quite deeply and passing this back to the top
level form may get a bit messy.

Does anyone have any recommendations for best practices on dealing with
this
type of error handling.

many thanks.

Andy



Nov 17 '05 #4
My personal opinion is that doing the latter makes for more readable and
flexible code.

There is no hard and fast rule. Using a custom exception that you catch at
the top level is one mechanism you may want to look at.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"aaj" <aa*@aaj.com> wrote in message
news:1117700217.56b280d8f3fd4f1d5c4bb10d66a84d08@t eranews...
Thanks guys

Nick, your quite correct, to give a bit of background, the GUI was just a
hang on from before the days when the code was automated (it runs as a
scheduled task now, and because of the Email and database logging doesn't
need any GUI, I just couldn't see the wood for the trees 8-)

that said though, both solutions seem to suggest handling the the errors
and passing them back up through the chain and exiting the App in its
natural place....

What I'm trying to ask (but not very well)... is if I have a class that
utilises another class, which inturn utilises another series of classes,
and one of the grandchild classes detects that something is wrong (not a
system fault, but some thing which is semi predictable) is it acceptable
(good/bad practice) to terminate the application right there in the
grandchild class (as there is no need for any more work to be done by the
app, nothing has been written yet, so no rewinding necessary etc..)

or

should the fact that a problem has been detected be passed all the way up
the chain to the control class where the rest of the code can be skipped
in a controlled manner and let the program terminate in its normal way.

Just as a bit of extra info, the reason I don't do all the checks before
proceeding with the importing of the data is purely down to efficiency.
Because the data is correct 98% of the time, I validate the data as it
streams in, rather than testing it and then streaming it in, and if
anomilies that should stop the import occur.

many thanks again, and I hope the above make sense

Andy

"aaj" <aa*@aaj.com> wrote in message
news:1117630892.5d1cb1393192df959e430d0640ff53f3@t eranews...
Hi all

I have an automated application, that runs in the middle of the night.

If certain 'non system' errors occur (things like malformed files,
missing
files etc..), I send an automatic Email and write a record to the
database.
This is handled in a class.

When these errors occur, once Emailed and written I want to just end the
App, simple as that.

My inital thought was to End the Application at the end of the error
handling class using Application.Exit or Environment Exit. this means I
just
write the code once and thats it, the App stops dead!! But while reading
up,
It seems to be bad practice.

Is a better way to set a flag within the Error handling class, work its
way
up the chain and then exit in the form code. The only problem is, some of
the error handling is nested quite deeply and passing this back to the
top
level form may get a bit messy.

Does anyone have any recommendations for best practices on dealing with
this
type of error handling.

many thanks.

Andy




Nov 17 '05 #5

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

Similar topics

2
by: WSeeger | last post by:
When creating a new class, is it encouraged to always include error handling routines within your LET and GET procedures? It's seems that most text books never seem to include much about error...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
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
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
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...
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.