码迷,mamicode.com
首页 > Windows程序 > 详细

C#中自定义异常

时间:2020-07-08 00:55:07      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:info   文件   pex   new   code   exce   using   OLE   row   

using System;

public class DataHouse

{

    public static void FindData( long ID) 

    {

        if( ID>0 && ID<1000)

            Console.WriteLine( ID );

        else

            throw new DataHouseException("已到文件尾");

    }

}

public class BankATM 

{

    public static void GetBalanceInfo( long  ID) 

    {

        try 

        {

            DataHouse.FindData(ID);

        }

        catch (DataHouseException e) 

        {

            throw new MyAppException("账号不存在",e);

        }

    }

}

public class DataHouseException:ApplicationException 

{

    public DataHouseException( string message )

        :base(message)

    {}

}

public class MyAppException:ApplicationException 

{

    public MyAppException (string message) 

        : base (message) 

    {}

    public MyAppException (string message, Exception inner)

        : base(message,inner)

    {}   

}

public class Test 

{

    public static void Main() 

    {

        try 

        {

            BankATM.GetBalanceInfo( 12345L);

        }

        catch(Exception e) 

        {

            Console.WriteLine ("出现了异常: {0}", e.Message);

            Console.WriteLine ("内部原因: {0}",e.InnerException.Message);

        }

    }

}

 

C#中自定义异常

标签:info   文件   pex   new   code   exce   using   OLE   row   

原文地址:https://www.cnblogs.com/wanshiliang/p/13264262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!