天天看点

Nearth===017/c#自定义异常处理编程练习

定义了一个具有异常处理能力的学生类——student类,该类包含两个私有变量成员:name和sid,分别表示学生姓名和身份证号,且name的长度不超过4个字节,sid的长度为18位;另外还定义各自的属性,设置为Name和Sid 。然后自定义一个异常类UserException,当对name所赋的值的长度超过4个字节或者对Sid所赋的值长度不为18位时都抛出此自定义异常。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//2、定义了一个具有异常处理能力的学生类——student类,
//该类包含两个私有变量成员:name和sid,
//分别表示学生姓名和身份证号,且name的长度不超过4个字节,
//sid的长度为8位;另外还定义各自的属性,设置为Name和Sid 。
//然后自定义一个异常类UserException,
//当对name所赋的值的长度超过4个字节或者对Sid所赋的值长度不为18位时
//都抛出此自定义异常。
namespace ConsoleApplication1
{
    class userException:Exception{
        public userException() { }
        public userException(string ms) : base(ms) { }
        public userException(string ms, Exception inner) : base(ms, inner) { }
    }
    class Student
    {
        private string name;
        private string sid;
        private string Name {
            set {
                name = value;
            }
            get{
                return name;
            }
        }
        private string Sid {
            set {
                sid = value;
            }
            get {
                return sid;
            }
        }
        public void setInfo(string name, string sid)
        {
            if (name.Length >4)
            {
                throw (new userException("姓名的长度超过了4个字节!"));
            }
            if (sid.Length!=11)
            {
                throw (new userException("身份证长度不满11位!"));
            }
            this.name = name;
            this.sid = sid;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student st = new Student();
            try
            {
                st.setInfo("张飞大人大","5111231997051627748");
            }
            catch(Exception e){
                Console.WriteLine("异常错误==>{0}",e.Message);
            }
        }
    }
}      

运行结果:

Nearth===017/c#自定义异常处理编程练习
Nearth===017/c#自定义异常处理编程练习

创建一个C#控制台应用程序,只接受 0至9之间的数字,如果用户试图输入0至9以外的字母或符号,则应显示适当的错误消息提示。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class userException:Exception{
        public userException() { }
        public userException(string ms) : base(ms) { }
        public userException(string ms, Exception inner) : base(ms, inner) { }
    }
    class Num {
        private int number;
        public void setNum(int number){ 
        if(number<0||number>9){
            throw (new userException("你输入的是0~9以外的字母或符号~~"));
        }
        if(number.GetType().ToString()!="System.Int32"){
            throw (new userException("你输入的不是数字~~"));
        }
        this.number = number;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Num num = new Num();
            int m;
            Console.Write("请输入一个0~9之间的数字:");
            m = Convert.ToInt16(Console.ReadLine());
            try
            {
                num.setNum(m);
            }
            catch (Exception e)
            {
                Console.WriteLine("产生异常:{0}", e.Message);
            }
            Console.ReadKey();
        }
    }
}      

开心满满的一天~~~~~~~~~~~~~~

继续阅读