1. Find the exception handling code with bad taste in the system
In the previous articleMiscellaneous Exception HandlingTry-catch-finallyThis section mainly introduces the concept of C # Exception Handling, exception design principles, basic knowledge, and so on. However, I am not particularly impressed with how to correctly use exception handling. In this article, I will analyze and explain how to correctly use exception handling code with bad taste in the previous system code.
1.1. Example 1
/// <Summary>
/// Save the record
/// </Summary>
/// <Param name = "entity"> entity </param>
Public Virtual Object save (T entity)
{
Isession session = nhibernateutils. getcurrentsession ();
Itransaction Tx = NULL;
Try
{
Tx = session. begintransaction ();
Object ID = session. Save (entity );
TX. Commit ();
Return ID;
}
Catch (hibernateexception ex)
{
If (TX! = NULL) Tx. rollback ();
Throw ex;
}
Finally
{
Nhibernateutils. closesession ();
}
}
In the code above
Catch (hibernateexception ex)
{
If (TX! = NULL) Tx. rollback ();
Throw ex;
}
Whether the code is designed with "if you understand the conditions that a specific exception is thrown in a given context, consider capturing these exceptions ." Or "Empty triggering is preferred when an exception is caught and thrown again. This is the best way to retain the abnormal call stack ." Which of the following statements do not meet the requirements? Change throw ex; To throw; or throw new repositorylayerexception ("An error occurred while saving the record! ", Ex); does it comply with the above two exception design principles?
1.2. Example 2
Presentation Layer Code
Try
{
Cursor = cursors. waitcursor;
Iapplicationcontext CTX = contextregistry. getcontext ();
Istorerepository storerepository = CTX. GetObject ("storerepository") as istorerepository;
Storeinfo store = store;
Store. Name = xxname. Text. Trim ();
Storerepository. Save (store );
}
Catch (exception ex)
{
Showmessagebox (ex, messagelevel. Error );
}
Finally
{
Cursor = cursors. Arrow;
}
Domain-Layer Code
Private string _ name;
/// <Summary>
/// Store name
/// </Summary>
Public Virtual string name
{
Set
{
If (string. isnullorempty (value ))
{
Throw new argumentnullexception ("value", "the shop name cannot be blank! ");
}
If (validationutils. getlength (value, charactertype. nvarchar) & gt; 200)
{
Throw new argumentoutofrangeexception ("value", "the shop name cannot exceed 200! ");
}
_ Name = value;
}
Get {return _ name ;}
}
Here, we need to explain that xxname is a text box control. If the value entered in the xxname text box often exceeds 200 characters, an error message box will pop up continuously. Will this cause efficiency problems, what do we think of when it comes to efficiency? By the way, the tester-doer mode is used. How can we apply this mode here? In fact, it is very simple. We only need to set this. xxname. maxlength = 200; does it solve this problem?
1.3. Summary
After careful analysis, we will find that there are a lot of code with bad taste in the system. Originally, we thought it was a correct approach in exception handling, and some beautiful practices found that there were problems. After studying and studying the exception handling system over the past few days, I have learned a lot about troubleshooting the abnormal handling practices in the system and found my own shortcomings, A lot of basic technical knowledge can be upgraded to the next level.