Java Exception Handling notes

Source: Internet
Author: User
Tags exception handling garbage collection

For a programmer who is very familiar with the C + + exception handling model, it can fully accept and easily use exception handling programming methods in the Java language without any other training and learning. This is because the exception-handling model in the Java language is nearly 99% similar to the exception handling model in C + +, both of which are almost identical in terms of syntax rules and semantics.

Of course, if you have more, or a deeper understanding of the exception handling model in the Java language, you can still find that there are many differences between the Java exception handling model and the exception handling model in C + +. Yes, the Java language is an improved version of the C + + language, so the exception handling model in the Java language will inevitably inherit the style and advantages of the C + + exception handling model. But, the good thing is not only needs to inherit the merit, more important is needs to "discard its dross, takes its essence", needs to develop!!! There is no doubt that the exception handling model in the Java language has fully reached this "development" height. It is more secure, more scalable, more powerful and richer than the C + + exception handling model.

The following content, a fool does not intend to give you a detailed description of Java exception handling programming of the specific syntax and rules. Because this is almost exactly the same as exception handling in C + +, these basics are detailed in too many books about Java programming. And Ahan thinks: here more needs is summary, need is the comparison, need is the emphasis is outstanding, need is the key place. So, the following focuses on the Java language in the exception handling model and C + + exception handling model to compare, let us thoroughly analyze what is the development of it? What are the advantages? What is the difference in detail with the C + + exception handling model? And why did you do it?

The try-finally syntax in the SEH anomaly model is introduced and brought into use

The biggest difference between the Java exception handling model and the exception handling model in C + + is that the try-finally syntax is introduced in the Java exception handling model, Ahan that it is borrowed from Microsoft's SEH. In the previous articles, detailed and in-depth elaboration of the SEH exception handling model, we learned that SEH is mainly designed for the C language, for the third manufacturer to develop a Window driver better security. At the same time, the SEH exception handling model, in addition to try-except to deal with the exception, there is also a try-finally syntax, which is mainly used to clear some of the resources have been allocated (due to abnormal appearance, which can not be released in the normal order, remember?) This is referred to as "unwind"), try-finally is somewhat similar to the function of destructor in object-oriented programming, because of the existence of this mechanism, it leads to the powerful and incomparable scenery of SEH.

The Java exception handling model now absorbs this design as well. But we know, whether in JAVA, or C + +, they have "destructor" Ah! They can be used to automatically release resources from the Object-oriented destructor! Yes! This is theoretically the case. But in practice, we may find or often encounter that only using destructors to release resources is not so good, for example, we often need to dynamically allocate objects from the heap, at which point the release object must explicitly call the Delete function to trigger the execution of the destructor of the object. If an exception occurs during this period, not only does the memory of the object in the heap not reach the result of the release, but some code that frees more resources in the destructor of the object cannot be executed. Therefore, this kind of consequence is very serious, this also calculates the C + + exception processing Model A relatively big flaw! (although C + + has other remedies, it is the use of "smart pointers" to ensure that some objects are released in a timely and efficient manner). In addition, there are many similar examples, such as closing a kernel handle (CloseHandle).

In the Java language, because of its use of garbage collection technology, it can effectively avoid the above mentioned in the similar to the C + + language, because of the exception caused by the lack of resources that can not be properly released embarrassing situation. However, it still introduces the try-finally syntax in its exception handling model, because, at any rate, it gives programmers great convenience, such as the following pieces of the program, to fully reflect how important try-finally is to improve the quality and beauty of our code.

import java.io.*;
/** *//**
author by http://www.bt285.cn http://www.5a520.cn
*/
public class Trans
{
public static void main(String[] args)
{
try
{
BufferedReader rd=null;
Writer wr=null;
try
{
File srcFile = new File((args[0]));
File dstFile = new File((args[1]));
rd = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), args[2]));
wr = new OutputStreamWriter(new FileOutputStream(dstFile), args[3]);
while(true)
{
String sLine = rd.readLine();
if(sLine == null) break;
wr.write(sLine);
wr.write("\r\n");
}
}
finally
{
// 这里能保证在何种情况下,文件流的句柄都得以被正确关闭
// 该方法主要用于清理非内存性质的资源(垃圾回收机制无法
// 处理的资源,如数据库连接、 Socket 关闭、文件关闭等等)。
wr.flush();
wr.close();
rd.close();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

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.