位置:首頁 > 軟件操作教程 > 編程開發(fā) > C# > 問題詳情

如何創(chuàng)建定制異常類?

提問人:劉冬梅發(fā)布時(shí)間:2020-10-12

using System;

public class NoDescException : ApplicationException

{

    public NoDescException() { }

    public NoDescException(string message) : base(message) { }

    public NoDescException(string message, Exception innerEx)

        : base(message, innerEx) { }

}


public interface IFun1

{ string ShowMe();}


public interface IFun2

{ string ShowMe(); }


class Circle : IFun1

{

    public string ShowMe()

    { return "Circle-IFun1"; }

}


public class ObjShowMe

{

    public static void ShowMe(object obj)

    {

        if (!(obj is IFun1 && obj is IFun2))

        {

            throw new NoDescException("Interface not implemented for " + obj.ToString());

        }

    }

}



public class MyApp

{

    static void Main()

    {

        Circle myCir = new Circle();

        try

        {

            ObjShowMe.ShowMe(myCir);

        }

        catch (NoDescException ex)

        {

            Console.WriteLine(ex.Message);

        }

    }

}

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部