如何創(chuàng)建定制異常類?
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);
}
}
}
點(diǎn)擊加載更多評論>>