C#自定义异常类

时间:2022-08-30 20:04:44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace TestException
{
    [Serializable]
    class LimsCustomException : ApplicationException
    {
        public LimsCustomException() { }
        public LimsCustomException(string message) : base(message) { }
        public LimsCustomException(string message, Exception inner) : base(message, inner) { }
        protected LimsCustomException(SerializationInfo info, StreamingContext context) : base( info, context) { }

    }
}

调用如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestException
{
    class Program
    {
        static void Main(string[] args)
        {
            try 
            {
                throw new LimsCustomException("信息不符合");
            }
            catch (LimsCustomException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message+"123");
            }

            Console.ReadKey();
        }
    }
}