Throwing and declaring exceptions in JAVA programs __java

Source: Internet
Author: User

throw an exception

in the previous article, we introduced the knowledge of catching exceptions. But you will certainly ask, since we can catch all sorts of anomalies, where are these exceptions thrown?

In the programming process, we often encounter this situation, in the current environment can not be resolved, such as user incoming parameter errors, IO device problems. At this point, the exception is thrown from the current environment and submitted to the superior for processing.

in the Java language, use the Throw keyword to throw an exception. Look at the following code Code:public class a**exception    {                  private String id;                                    public void  SetID (string id)                  {                              if (Id.length ()  == 7)                              {                                     this.id =  id;                              }else                            {                                      throw  new illegalargumentexception ("parameter length is not 7 bits");                             }                 &NBSP}   }  

The above code, we throw a illegalargumentexception type of exception.

declaring an exception

If an exception is thrown in a method body, we want the caller to be able to catch the exception. So, how do you notify the caller?

In the Java language, the throws keyword declares an exception that a method might throw, in the above code we throw a illegalargumentexception type of

exception. By doing the following, you can let the caller know what kind of exception the method might have and catch it in time. Code:public class a**exception    {                  private String id;                                    //uses the throws keyword to declare an exception that may occur with this method          public void setid (string id)  throws  Illegalexception                 {                                if (Id.length ()  == 7)                                {                                      this.id = id;                                }                               else                             {                                       throw new illegalarguMentexception ("parameter length should be 7");                              }                   &NBSP}   }  

 

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.