Simplifying exception security in Java and C #

Source: Internet
Author: User

In the C cluster language, C ++ introduces exceptions first. When the C ++ Standards Committee voted, the British representative raised an objection, saying that exceptions almost destroyed every program. The following code snippets demonstrate how exceptions mess up things.

Void f ()
{
T * p = new T;
// Some processing
Delete p;
}

 

If an exception is thrown during the processing, the statement "delete p;" will not be executed, resulting in Memory leakage. To solve this problem, we have two proposals: "smart pointer" and garbage collection.

The opposition of the British makes sense. Writing exceptional security code in C ++ has plagued the C ++ community for several years, from 97 to 99 years, finally, I summarized some good solutions. So far, it is still very difficult.

Java and C # are popular languages. Most programmers are hard to master such difficult technologies as exception security. Therefore, some simplified methods are introduced.

Java introduces the garbage collection technology, so programmers don't have to worry about memory collection. However, there are other resources, such as database connections, file handles, Mutex, and so on. In Java, the synchronized keyword (similar to lock in C #) is introduced to simplify the use of Mutex. As follows:

Public synchronized void f1 (List list ){
Synchronized (list ){
// Do something
}
}

In C #, it is a further step. To solve abnormal security problems, two keywords lock and using are introduced to manage resources. Lock corresponds to synchronized in Java.

Object obj = new Object ();
Lock (obj)
{
// Do something
}

The above code is written in this way when no lock is needed:

System. Threading. Monitor. Enter (obj );
Try
{
// Do something
}
Finally
{
System. Threading. Monitor. Exit (obj );
}

Using is a special method used in combination with IDisposable. It may be an innovation.
Let's first look at the IDisposable interface:

Public interface IDisposable
{
// Methods
Void Dispose ();
}

Let's take a look at the implementation of data connection SqlConnection. SqlConnection implements the IDisposable interface, which is roughly implemented in SqlConnection as follows:

If (disposing)
{


This. Close ();
}

The Close method is called in Dispose and resources are released. File-related objects are also processed in a similar way.

Use Using:

Using (IDbConnection conn = new SqlConnection ())
{
// Do something
}

If you do not use Using, you also need to ensure exceptional security. It can be written in this way, which is equivalent to the above Code:

IDbConnection conn = new SqlConnection ();
Try
{
// Do something
}
Finally
{
IDisposable disposableObj = conn as IDisposable;
If (disposableObj! = Null)
{
DisposableObj. Dispose ();
}
}

Of course, the more direct code is:

IDbConnection conn = new SqlConnection ();
Try
{
// Do something
}
Finally
{
Conn. Close ();
}

In C #, the usage of using is not far away. It is actually used in combination with the try... finally method and the IDiposible interface.

In fact, the foreach keyword also simplifies the compilation of abnormal security code:

IList list = new ArrayList ();
Foreach (Object obj in list)
{
// Do something
}

Actually equivalent:

IList list = new ArrayList ();
IEnumerator iter = list. GetEnumerator ();
Try
{
While (iter. MoveNext ())
{
Object obj = iter. Current;
// Do something
}
}
Finally
{
IDisposable disposableObj = iter as IDisposable;
If (disposableObj! = Null)
{
DisposableObj. Dispose ();
}
}

In foreach, try... finally is also used to implement the IEnumerator object of IDisposable for Dispose.

Anders Hejlsberg once said that there will be a lot of try... finally code in excellent code. To reduce the use of try... finally, C # introduces lock, using, and foreach.

However, exception specification is missing in C #, which is a pity. Anders Hejlsberg once published some articles, which makes sense, however, it is regrettable that C # has no exception specification.

Related Article

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.