How to write a program without Try/catch

Source: Internet
Author: User
Tags exception handling

One, exception handling is not simple

Personally feel that exception handling for programmers, especially for those beginners. NET programmer, is the most familiar, but also the most difficult to master. Say it's familiar, because it's just try/catch. Say it is difficult to grasp, many developers are not clear where try/catch should be placed? Under what circumstances should the exception be logged? Under what circumstances should an exception be encapsulated? Under what circumstances do I need to replace the exception? Under what circumstances will it be necessary to throw the caught exception again? Under what circumstances is not required. In short, exception handling is not as simple as we think.

Exception handling is essential for any type of application. Reasonable exception handling should be scene driven, in different scenarios, the exception handling strategy is often different. The strategy of exception handling should be configurable, because what happens to an application is often unpredictable, and the inadequacy of existing exception strategies often needs to be reflected when something really happens, so we need a dynamically configurable way to maintain the exception handling strategy. At present, some open source exception handling framework provides this configurable, scene-driven exception handling, Enterlib exception Handling Application Block is a good choice.

Second, exception handling is transparent to the eventual developer

"Exception handling is transparent to the ultimate developer," probably a bit too much. However, in my personal project experience, this is an ideal state. Because the exception policy is typically configured dynamically, it does not need to be reflected above the code. If the implementation of exception handling can be provided through the framework, so that developers do not need to write any exception handling code, only need to focus on the implementation of the business process can not only improve the efficiency of development, but also to improve the maintainability of the system.

Our current project is a typical distributed application in which all business process processing and data access are implemented on the server side and eventually exposed to the client (Smart client) and third party applications in the form of WCF services. All clients and services end logically with a corresponding hierarchy, but exception handling is only implemented in two places, one place is the WCF service itself, and the other implements the UI layer. Forgetting to make it clear, our project directly Enterlib exception Handling Application Block as our exception handling framework. For server-side exception handling, we implement WCF and Ehab integration (WCF and exception handling Appblock integration), so no developers are required to add any Try/catch code. But the client, for the event of a control, because the UI itself is at the top of the entire call stack, it is difficult to implement dynamic injection of exception handling via an AOP blocking mechanism, so the client appears very similar to the try/catch shown in the following code.

1: private void buttonCalculate_Click(object sender, EventArgs e)
2: {
3:   try
4:   {
5:     //
6:   }
7:   catch (Exception ex)
8:   {
9:     if (ExceptionPolicy.HandleException(ex, "policyName"))
10:     {
11:       throw;
12:     }
13:   }
14:
15: }

I'm a compulsive person with repetitive code, and I have the urge to refactor code to see two of the same code, not to mention that so many of the same code fills the client.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.