472,328 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 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 2409
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...
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...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.