Personally feel that exception handling for programmers is the most familiar and the most difficult to master. Say it's familiar, because it's just try/catch/finally. It's difficult to grasp, because many developers are not sure where try/catch/finally should be located? 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 it not necessary?
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, EntLib Exception Handling Application Block (hereinafter referred to as Ehab) is a good choice. [Source code download from here]
First, respond to requests by specifying handle-error-action
Before we get into the formal introduction of how to implement and EntLib to automate exception handling, let's take a look at the "Automation" features of exception handling. As an example of a user login scenario, we defined a simple data type in a Web application created through the ASP.net MVC project template in Visual Studio Logininfo encapsulates the user name and password required for user login.
1:public class Logininfo
2: {
3: [DisplayName ("username")]
4: [Required (errormessage= "Please enter {0}")]
5: Public string UserName {get; set;}
6:
7: [DisplayName ("password")]
8: [Required (errormessage = "Please enter {0}")]
9: [DataType (Datatype.password)]
Public string Password {get; set;}
11:}
Then we define a homecontroller as follows. The Http-get action Method index will present a user login view that uses the created Logininfo object as its model. The true user authentication logic is defined in another index method that has the Httppostattrubute attribute applied: If the user name is not foo, throw a invalidusernameexception exception, or if the password is not "password", Throws the invalidpasswordexception exception. Invalidusernameexception and Invalidpasswordexception are two of our custom exception types.
1: [Exceptionpolicy ("Defaultpolicy")]
2:public class Homecontroller:extendedcontroller
3: {
4:public actionresult Index ()
5: {
6:return View (New Logininfo ());
7:}
8:
9: [HttpPost]
: [Handleerroraction ("Onindexerror")]
11:public actio Nresult Index (Logininfo logininfo)
: {
13:if (string. Compare (Logininfo.username, "foo", True)!= 0)
: {
15:throw new invalidusernameexception ( );
16:}
17:
18:if (logininfo.password!= "Password")
: {
20:throw new I Nvalidpasswordexception ();
21:}
22:return View (logininfo);
23:}
24:
: [HttpPost]
26:public actionresult onindexerror (logininfo logininfo)
27: { br> 28:return View (logininfo);
29:}
:}