272 Custom Exceptions

6a4bfe6bf2db4b50a4ebfd04d069db8f.png

bab1185f7c66420cb5c413e2bb6a0158.png

示例

Program.cs

using System;

namespace CustomExceptionExample
{
    class BankAccount
    {
        public int AccountNumber { get; set; }
        public string AccountHolderName { get; set; }

        private double _currentBalance;
        public double CurrentBalance
        {
            get => _currentBalance;
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException($"The value of 'CurrentBalance' should be a positive number. You have supplied {value}", "CurrentBalance");
                }
                _currentBalance = value;
            }
        }
    }


    //custom exception
    class InsufficientFundsException: InvalidOperationException
    {
        public InsufficientFundsException()
        {
        }

        public InsufficientFundsException(string message): base(message)
        {
        }

        public InsufficientFundsException(string message, Exception innerException) : base(message, innerException)
        {
        }
    }

    class FundsTransfer
    {
        public void Transfer(BankAccount sourceAccount, BankAccount destinationAccount, double amount)
        {
            try
            {
                if (amount < 0 || amount > 10000)
                    throw new ArgumentOutOfRangeException("amount", 10000, "For funds transfer, the value of 'amount' should be between 1 to 10000");
                
                if (amount > sourceAccount.CurrentBalance)
                    throw new InsufficientFundsException($"Insufficient balance in the source account. The current balance is {sourceAccount.CurrentBalance}. But the amount to transfer is {amount}");
                sourceAccount.CurrentBalance -= amount;
            }
            catch (NullReferenceException ex) //it catches NullReferenceException
            {
                throw new ArgumentNullException("You have supplied null value to 'sourceAccount' parameter", ex); //it throws the same object of ArgumentNullException
            }

            try
            {
                destinationAccount.CurrentBalance += amount; //it throws a new object of NullReferenceException
            }
            catch (NullReferenceException ex) //it catches NullReferenceException
            {
                throw new ArgumentNullException("You have supplied null value to 'destinationAccount' parameter", ex); //it throws the same object of ArgumentNullException
            }
        }
    }
    class Program
    {
        static void Main()
        {
            try
            {
                BankAccount bobBankAccount = new BankAccount() { AccountNumber = 101, AccountHolderName = "Bob", CurrentBalance = 7000 };
                BankAccount aliceBankAccount = new BankAccount() { AccountNumber = 102, AccountHolderName = "Alice", CurrentBalance = 5000 };
                BankAccount stevenBankAccount = null;

                FundsTransfer fundsTransfer = new FundsTransfer();
                fundsTransfer.Transfer(bobBankAccount, stevenBankAccount, 9000);
                Console.WriteLine("Funds Transferred");
            }
            catch (ArgumentNullException ex) //it catches the object of ArgumentNullException which was thrown in either of the catch blocks of 'Transfer' method
            {
                Console.WriteLine(ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine(ex.InnerException.Message);
            }
            catch (ArgumentOutOfRangeException ex) //it catches the object of ArgumentOutOfRangeException which was thrown in the 'Transfer' method
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.ActualValue);
                Console.WriteLine(ex.ParamName);
            }
            catch (ArgumentException ex) //it catches the object of ArgumentException which was thrown in 'Transfer' method
            {
                Console.WriteLine(ex.Message);
            }
            catch (InsufficientFundsException ex) //it catches the object of InsufficientFundsException which was thrown in 'Transfer' method
            {
                Console.WriteLine(ex.Message);
            }
            catch (InvalidOperationException ex) //it catches the object of InvalidOperationException which was thrown in 'Transfer' method
            {
                Console.WriteLine(ex.Message);
            }
            
            Console.ReadKey();
        }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄健华Yeah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值