开发者

Can I apply some code like catch, if database is closed or could not connect, application should not be halt and exit

I have built a C# desktop application, its database is online. Some times because of internet it becomes disconnected and as a result if user saves a form, an error of db disconnection occurs and it get closed and exits. This is quite horrible.

Is there an option that I can apply try catches every where in the code, but the issue is, application is so big that it will become a headache, connection string is one, but connections made are many, can I apply some configuration like that, if database is closed it should not be halt and exit, what is the easiest 开发者_StackOverflowsolution.

Thanks Atif


Looks like you've to start with refactoring and introduce decoupled Data Access Layer which is aware of work with a data base. Then life would be much easier ;)

This is a common suggestion which saves you a 80% of time in 20% of efforts.


Fastest solution is implement try catch in your program.cs method Main()

Best solution is to refactor your software, search for all connections using search in VS and replace it with new logic. For Example

  DataHelper.Load("conn_string_name", "query", sqlparameters);

and handle all errors inside.


Exception handlers are built for this very purpose.

Exceptions that are not caught will result in the application being terminated because something unexpected has happened and the app is in a volatile state. This "fail-fast" behavior is by design because it'll help you track down precisely where your apps fail unexpectedly and won't leave your app in an unknown state.

Don't wrap every DB operation with an Exception Handler. Instead, consider adding exception handlers for specific issues at strategic points in your app, but only if you're able to actually do something about the errors raised. There's no point in catching an error unless you're actually able to remedy the problem and/or take remedial steps.

There are some good recommendations for correctly handling exceptions here


In Winforms apps, there is an event on the Application static object called ThreadException. If you handle that event, you will receive all exceptions that are about to be thrown out of the application's main thread. This is generally a good place to catch exceptions by type when said exception types could be thrown out from many places in the app.

However, it does have its limits. It works for the main thread only (so anything that throws an exception out of the thread pool won't be caught here). If you've designed your app well, you're probably using background worker threads for data access, leaving the main thread (which handles the UI) free to keep the app responsive.

As was suggested, you can also abstract your data access into its own layer of the program's architecture, with a few main driving functions to perform the actual DB reads/writes. Wrap those read-writes in try-catch blocks that will retry transport-level errors, timeouts, etc, and you should be good to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜