.net C#用户定义的异常

kcugc4gi  于 2023-03-09  发布在  .NET
关注(0)|答案(4)|浏览(131)

如果一个类定义了用户定义的异常,而这个异常扩展了C#中的System.exception类,那么是否有必要在“public”可见性模式下声明该类?

xxslljrj

xxslljrj1#

这完全取决于您希望如何使用用户定义的异常类。
访问修饰符的概念与用户定义异常的概念完全无关。
用户定义的异常只是扩展System.Exception的用户定义的类,而访问修饰符是指定该类相对于客户端代码的可见性的构造。
这意味着如果你只想在定义程序集中使用你的自定义异常类,你可以简单地把它定义为一个内部类。当然,这不是很有用,因为你通常在类库中定义自定义异常类,并且你希望它们在引用你的类库的任何程序集中都是可见的。以便使用者有机会处理您的自定义异常类(如果它在他或她的客户端代码中有意义的话)。

68bkxrlz

68bkxrlz2#

Try it on DotNetFiddle并参见:

public class Foo
{
    private class MyException : Exception
    {
        public MyException(string message) : base(message) { }
    }

    public static void Throw()
    {
        throw new MyException("Hello world.");
    }
}
public class Program
{
    public static void Main()
    {
        try
        {
            Foo.Throw();
        }
        //catch(Foo.MyException myException)
        //{
        //     This doesn't compile
        //}
        catch(System.Exception exception)
        {
            Console.WriteLine
            (
                "Exception is of type '{0}' with a message of '{1}'", 
                exception.GetType().Name, 
                exception.Message
            );
            //Does not compile:
            //var typedException = (Foo.MyException)exception;  
        }
    }
}

输出:

Exception is of type 'MyException' with a message of 'Hello world.'

所以你仍然可以捕获异常,检查它的类型,读取它的基属性,一切都正常,但是如果你想以类型安全的方式处理它,并将它强制转换为特定的类型,你的代码将无法编译,这也意味着你不能使用类型特定的catch处理程序。

kyxcudwk

kyxcudwk3#

using System;
namespace EXP7{
    public class InsufficientBalanceException : Exception
    {
        public InsufficientBalanceException(string msg) : base(msg)
{}
}
public class UserDefinedException
{
        double minbal;
        double balance;
        double withdraw;
        public UserDefinedException(double m, double b, double w)
        {
            minbal = m;
            balance = b;
            withdraw = w;
        }
        public void checkBalance() {
        try
        {
            if ((balance == minbal) && (withdraw <= minbal || withdraw > minbal))
            {
                throw new InsufficientBalanceException("Withdraw amount cannot be less than or equal to minimum balance!");
            }
            else {
            
            balance -= withdraw;
            Console.WriteLine("New balance: "+balance);
            }
        }
        catch (InsufficientBalanceException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine($"Current balance:{balance} ");
        }
    }
}
    class Program {
    public static void Main(string [] args) {
        UserDefinedException obj = new UserDefinedException(1000, 1000, 2000);
        obj.checkBalance();
    }
}
}
kmb7vmvb

kmb7vmvb4#

using System;
namespace bankbalance{
class Program
{
    static void Main(string[] args)
    {
        double balance = 1000;
        Console.Write("Enter amount to withdraw: ");
        double withdrawAmount = double.Parse(Console.ReadLine());

        try
        {
            if (withdrawAmount > balance)
            {
                throw new Exception("Withdraw amount cannot be greater than balance!");
            }
            balance -= withdrawAmount;
            Console.WriteLine("New balance: "+balance);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine($"Current balance:{balance} ");
        }
    }
}
}

//由kamalesh和sharad编写的代码

相关问题