Java Custom Exceptions

Source: Internet
Author: User
Tags define exception try catch

The project is a lot of anomalies we have to define ourselves, if the Java to write a good exception to do those projects, what kind of NULL pointer error and so on is completely useless.

If you use Java to define these exceptions, such as member login, the user to login to your membership, the user input error if the reminder is a null pointer error or something wrong, he can not understand what the meaning. The user has not learned to program without knowing what is the null pointer error and what is the type conversion error they do not know.

So Java gives us programmers the ability to define exception methods on their own. If you define the exception, the user does not fill in the account number or password will prompt to enter the account password, if the member does not support the input of English account will be prompted not to support the English alphabet enter the digital account number and so on, this is the benefit of the custom method. So we have to use custom exceptions in future projects, although the project is a bit troublesome but it is more clear what is the meaning of positioning errors. 9.3 is shown below:

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

Figure 9.3

If new runtimeexception he doesn't need to be surrounded, 9.31 shows:

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

Figure 9.31

Why? Because in Java the meaning is: RuntimeException in Java has already been as a capture, so usually write his time do not need to force a try catch can also. And exception if he encounters a code that uses exception to be wrong, then it will force the requirement to write a try catch surrounded. Custom exceptions either inherit exception or inherit runtimeexception.

The method throws an exception: the exception throws out and does not handle the error itself. His declaration format is that this method of writing throws Exception surface in the method may produce a Exception error. Throws is the declaration of error, is to tell someone this method may have exception error please write try catch to solve this problem can also be placed in their own definition of the exception error.

We write the function of the method must be thrown out to be able to secretly solve, if the user encountered a problem login do not go into what reflects all the time in the window does not move, he never know this method error or error. He's not going to know where the problem is. He will think that your project is in trouble because you have solved it yourself. So our exception must be thrown out and let the user solve it himself. The exception is to throw him out and tell others that there's been a mistake.

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/1872636

Java Custom Exception

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.