c语言宏定义####_C#中的用户定义异常

c语言宏定义####

C#用户定义的异常 (C# User-defined exceptions)

In C#, we can create our exception class by inheriting base class Exception. Because as we know that Exception is the base class for all types of exceptions in C#.

在C#中,我们可以通过继承基类Exception创建异常类 。 因为我们知道Exception是C#中所有类型的异常的基类。

Here is the example of a user-defined exception,

这是用户定义的异常的示例,

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);
        }
    }
}

Output

输出量

User Define Exception
Exception of type 'UserDefineException' was thrown. 

In the above program, we created a class "UserDefineException" by inheriting the base class "Exception" and created a parameterized constructor, through the user-defined object in "try block" of "Program" class, it is caught in "catch block".

在上面的程序中,我们通过继承基类“ Exception”创建了一个“ UserDefineException”类,并通过“ Program”类的“ try块”中的用户定义对象创建了一个参数化构造函数,将其捕获在“ catch块”中“

Read more:

阅读更多:

翻译自: https://www.includehelp.com/dot-net/user-defined-exceptions-in-csharp.aspx

c语言宏定义####

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值