C#中的用户定义异常
C#用户定义的异常
在C#中,我们可以通过继承基类Exception来创建异常类。因为我们知道Exception是C#中所有类型的异常的基类。
这是用户定义的异常的示例,
using System;
class UserDefineException: Exception
{
public UserDefineException(string str)
{
Console.WriteLine(str);
}
}
class Program
{
static void Main()
{
UserDefineException M = new UserDefineException("User Define Exception");
try
{
throw M;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}输出结果
User Define Exception Exception of type 'UserDefineException' was thrown.
在上述程序中,我们通过继承基类“Exception”创建了一个“UserDefineException”类,并通过“Program”类的“try块”中的用户定义对象创建了一个参数化构造函数,将其捕获在“catch块”中“。
阅读更多:
C#中的异常处理
具有多个catch块的C#异常处理
热门推荐
10 圣诞祝福语简短小学
11 祝妹妹毕业祝福语简短
12 开店大吉文案祝福语简短
13 女神节日简短祝福语大全
14 航空公司祝福语简短
15 参加儿子大学祝福语简短
16 锡婚祝福语简短霸气
17 潮汕生日谚语祝福语简短
18 少先队代表送祝福语简短