English 中文(简体)
Long running App, how handle Errors?
原标题:

i have to implement a Info Terminal. I choose dot.net and the terminal is only a touchpad. So this System running 7 days 24 hours.

So i call a Webservice, display Data, show Website stuff. Many things can going wrong.

Have you some recommendations for this scenario? Every function in an try catch? AppDomain.CurrentDomain.UnhandledException event?

Thanks Andreas

最佳回答

Basically, you should handle any error as soon as it is possible - so if you re calling webservice wrap all the calls in a try/catch block and handle the error there - you can, for example, log the exact error, aggregate many webservice-related exception in more generic, DataSourceFaultException (name is only for example), which will be then received by UI and UI will be able to easily determine, that it can t display requested info because communication failed, and choose to retry, notify user or do anything else.

However - with long running application there are many more errors you ll have to deal with. Many of them are not easy to predict, as they re not necessarily related to any specific call - you can run out of memory, a recursion might cause stack overflow, a system timer can reach it s max value and start from the beginning etc.

You shouldn t handle those errors in every method, as it will only hurt code readibility and will be error prone. Those errors are best handled by UnhandledException event. However, you must remember, that when the exception reaches UnhandledException event, you cannot assume anything about state of your application - the error might have corrupted some (or even all) of internal state. So when such condition occurs, it s best to try to create an error log and gracefuly restart the application (not necessarily whole application - maybe it will be possible to reinitialize application s state - if so, that s a valid option too. However, you must be aware that you won t be able to recover from some errors and handle such situation anyway).

问题回答

It depends.

If you can handle appropriately an exception within a function - handle it. If not - create a global exception handler to inform user or log it.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签