7. Unit III Task Four training: Method SetID can set a value for a class's properties: ID (ID number), when given a value of length of 18 o'clock, assigned to the ID, the duty length is not 18 o'clock, throw illegalargumentexception exception. In Mai

Source: Internet
Author: User
Tags throw exception

7. Unit III Task Four training: Method SetID can set a value for a class's properties: ID (ID number), when given a value of length of 18 o'clock, assigned to the ID, the duty length is not 18 o'clock, throw illegalargumentexception exception. To capture and handle exceptions in the main method, write programs to achieve these functions.


The public class Test {
	private String id;//ID number should be the length of public
	void SetId (String id) {
		if (id.length () = =) {
 //determine if the length of the ID number is
			this.id = ID;
		} else {
			throw new IllegalArgumentException ("The parameter length should be 18.") ");	Throw exception
		}
	} public
	
	static void Main (string[] args) {
		test  te = new test ();
		try {
			Te.setid ("0123456789123456789");
		} catch (IllegalArgumentException IE) {		//capture and handle exceptions
			System.out.println (Ie.getmessage ());
		} finally{
			System.out.println ("End");}}




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.