Java Exception Handling

Source: Internet
Author: User
Tags throwable try catch

Exception He is not a mistake and a reminder, in order to resolve some abnormal errors. There are mistakes do not fret, to maintain a good mentality, the most important of the state of mind has encountered a mistake to feel the time to learn from the experience. Just keep it up so that you can solve the problem and how it happened.

Find the wrong code first look at the class name, do not look at those useless things, error, as long as you find the name of the class you know, and then click on him to find the error, if found that the error but still do not see where the problem in the end then you can put the wrong code to comment out and then run him. You can also use Print System.out.pritnln () to print the wrong code.

Exception family: The ancestor of the Anomaly class was throwable in which he was born with error (Error) and exception (exception). Exception inside was born again RuntimeException (runtime exception). 9.1 is shown below:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/d3687455a19d4395a1331f2a743e4b38.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.1

Each error will have a corresponding exception class because each error is represented by the class name, so each error requires a custom exception class, Throwable He is the biggest error class, error He is to solve the more serious errors, exception is to solve some less serious errors, RuntimeException is a more non-serious error.

There was an exception. How can we catch the error and how to resolve the exception error. For example, tourist attractions in his design there will be some rescue station if there is a problem, then the rescue station will be immediately captured for treatment.

In the program if there is a problem with the program to catch the problem of treatment, then he invented a try catch statement. This statement is a small capture of the structure of the statement, try inside is a possible error code, catch is a trap, that is, if the error code will jump into the catch in the rescue. For example, browse the scenic try inside surrounded by this scenic spot, if someone infected with pollen then will enter catch inside catch is like a trap dug well waiting for treatment. Different problems have different traps waiting for treatment. If someone is beaten, they will enter the catch trap to be treated. 9.11 is shown below:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/621d7feddbaa45128f8869f9b26116e2.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.11

As long as there is a problem to the rescue station inside run can be, our program is so, the code has been wrong so it is necessary to dig out the corresponding traps, one but the code is wrong to go into the code inside the line. So the trap is ready to capture code that could be faulted in this try, and can write multiple traps. 9.12 is shown below:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/08b26e8280b54122b7fc925909fc399b.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.12

10/0 is wrong to report that ArithmeticException cannot be divisible by zero. Then we'll wrap it up in a try and catch it in a trap that can't be divisible by zero, as shown in 9.13:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/6e358fc8182349d8abd0d6f91dc3cc1d.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.13

If there is an error in the execution of the procedure, the error starts to throw into the trap, and the following code no longer executes 9.14:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/b44bb642514c489ab1935cd2e11a09e3_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.14

Just make a mistake. The following code will not be executed anyway, want to let the following code execution then it must be no error to execute, no error then he will not jump to catch inside 9.15:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/5bf839bae3e3465dad1191993f2f04ff_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.15

Catch is a prevention area, prevent him from problems, if there is a problem, immediately capture processing.

If there is another error with a null pointer error, but the trap is not caught, then his error will still be captured 9.16:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/41854b2be3da4099ab972949441b8e73_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.16

Because there is no trap to handle the null pointer, he also dug a null pointer to the trap to receive his error 9.17 as shown:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/58025c56d198490ead0e191b982d77fa_th.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.17

There is something wrong in the code that will find the error handling, and this is his error handling mechanism. An anomaly is a predictive prediction that can occur, that is, that the code will go wrong and then wrap it up.

What if there are other errors in the code? Then if there is an error can be dug in the back of a exception trap, this trap is exception common trap as long as the exception error can come in. Exception this trap can only be written in the back, because he is the parent class, all with the suffix of exception is inherited exception or runtimeexception.

So is it not necessary to dig the traps in the future? Using a generic trap directly? In fact, it's not such a common pitfall, but it's a waste of time to figure out what's wrong, so sometimes it's going to be distracting to know exactly which code is wrong.

Why do some code have to be surrounded by a try catch? If this error message inherits the RuntimeException error as the parent class then he can not force the person to write a try Catch statement if his error directly inherits Throwable, error, Exception so if you call this method, he has an error and must be forced to enclose it with a try catch.

If it's a memory overflow then it's no use if you use exception's generic to capture him, because memory overflows he is error 9.18, as shown in:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/2025ae0a299e4fafa24e062fa72a9b59.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.18

Why did catch not catch his mistake? Because this is an error that is more serious, you need to use error to capture his 9.19:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/4e903f26b4a74e1ea79cebefabcbb90e.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.19

If you want to capture all the exception errors then write the exception. If you want to capture the error then write the error. If the 2 series of errors want to capture then use throwable he can catch all the errors are infinite, if you do not know the error is wrong or exception, then you can use Throwable to capture.

Try Catch nesting:

Nested try can have no catch, and if there is no catch then there must be a finally. Try catch nesting, if there is no corresponding trap capture, then the outside trap will be found to capture. 9.110 is shown below:

650) this.width=650; "src=" Http://img.mp.itc.cn/upload/20161028/8c224521d6f344d1ad96d91ab0909d69.jpg "style=" border:0px;margin:0px;padding:0px;font-size:0px; "/>

Figure 9.110

He is like this, encountered the mistake he sees is not the corresponding error, is not then throws out, throws in the corresponding trap inside. Nested try catch does not have a corresponding trap then look outside, so this is thrown to the upper level.

Technology sharing: www.kaige123.com

This article is from the "11247808" blog, please be sure to keep this source http://11257808.blog.51cto.com/11247808/1871470

Java Exception Handling

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.