-
如何在C#入门经典教程中创建自定义异常类
资源介绍
如何创建定制异常类
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);
}
}
}
- 上一篇: 定制处理未处理异常-C#入门经典教程
- 下一篇: 调试的必要性-C#入门经典教程