SoFunction
Updated on 2025-03-07

Talking about exception handling try catch finally

1. Find the exception handling code for bad taste in the system

In the previous article, I talked about exception handling try-catch-finally, I mainly introduced the concepts of C# exception handling, exception design guidelines, basic knowledge and other aspects, but I am not particularly impressed with how to use exception handling correctly. In this article, I analyze and explain how to use exception handling correctly by looking for exception handling codes with bad smells in previous system codes.

1.1. Example 1

/// <summary>
/// Save record/// </summary>
/// <param name="entity">entity</param>public virtual object Save(T entity)
{
ISession session = ();
ITransaction tx = null;
try
{
tx = ();
object id = (entity);
();
return id;
}
catch (HibernateException ex)
{
if (tx != null) ();
throw ex;
}
finally
{
();
}
}

Among the above code

catch (HibernateException ex)
{
if (tx != null) ();
throw ex;
}

Does the code in exception design not quite match the requirements of "If you understand the conditions raised by a particular exception in a given context, consider catching those exceptions." or "When catching and throwing an exception again, it is preferred to use null raising. This is the best way to keep the exception call stack." Does it conform to the above two exception design criteria?

1.2. Example 2

Presentation layer code

try
{
Cursor = ;
IApplicationContext ctx = ();
IStoreRepository storeRepository = ("StoreRepository") as IStoreRepository;
StoreInfo store = Store;
 = ();
(store);
}
catch (Exception ex)
{
ShowMessageBox(ex, );
}
finally
{
Cursor = ;
}

Domain layer code

private string _name;
/// &lt;summary&gt;
/// Shop name/// &lt;/summary&gt;
public virtual string Name
{
set
{
if ((value))
{
throw new ArgumentNullException("value", "Shop name cannot be empty!");
}
if ((value,) &gt; 200)
{
throw new ArgumentOutOfRangeException("value", "The store name cannot be greater than 200!");
}
_name = value;
}
get { return _name; }
}

It is necessary to explain here that XXName is a text box control. If the value input in the XXName text box often exceeds 200 characters, an error message box will pop up constantly. Will this cause efficiency issues? What will we think of when it comes to efficiency issues? By the way, it is the Tester-Doer mode. Haha, how do we apply this mode here? In fact, it is very simple. We just need to set = 200; will we solve this problem?

1.3. Thinking and Summary

If you analyze it carefully, you will find that there are quite a lot of bad codes in the system. It turns out that the methods you think are more correct and more beautiful methods are found to be problematic. After studying and studying the system of exception handling in the past few days, I have learned a lot of knowledge and found my shortcomings. It turns out that a lot of basic technical knowledge can be further improved.

Related readings:Miscellaneous Try-catch-finally exception handlingThank you very much for your support for my website.