Exception Handling and custom exception learning notes

Source: Internet
Author: User

 

1 public class person
2 {
3 private int _ age;
4 Public int age
5 {
6 set {
7 if (value <0 | value> 150)
8 {
9 throw new ageformatexception ("the age input is incorrect! ");
10}
11 else
12 {
13 This. _ age = value;
14}
15}
16 get {return this. _ age ;}
17}
18 public class ageformatexception: formatexception // custom exception
19 {
20 public ageformatexception ()
21: Base ()
22 {}
23 public ageformatexception (string message)
24: Base (Message)
25 {}
26
27}
28 public static void main (string [] ARGs)
29 {
30 person P = new person ();
31 While (true)
32 {
33 try
34 {
35 console. writeline ("Enter age :");
36 int intage = convert. toint32 (console. Readline ());
37 p. Age = intage;
38 console. writeline ("this person's age:" + P. Age );
39}
40 catch (overflowexception oex)
41 {
42 throw oex;
43}
44 catch (formatexception Fex)
45 {
46 throw Fex;
47}
48 catch (exception ex)
49 {
50 throw ex;
51}
52}
53}
54}

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.