273 Stack Trace

示例

Program.cs

using System;

namespace StackTraceExample
{
    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);
                Console.WriteLine(ex.StackTrace);
            }
            catch (InvalidOperationException ex) //it catches the object of InvalidOperationException which was thrown in 'Transfer' method
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }
    }
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C中的stacktrace是指在程序运行过程中,记录函数调用关系和执行位置的一种机制。当程序发生错误或异常情况时,可以通过stacktrace来定位问题发生的位置,帮助开发人员进行调试和错误修复。 在C语言中,stacktrace可以通过获取函数调用栈来实现。函数调用栈是一个存储函数调用关系和局部变量的数据结构,栈顶表示当前正在执行的函数。可以通过以下几种方法来获取C的stacktrace: 1. 使用调试工具:在开发环境中,可以使用调试工具(如GDB)来获取stacktrace。通过设置断点或捕捉异常,可以在程序执行过程中暂停并查看函数调用栈的信息。 2. 异常处理:在代码中可以使用异常处理机制来捕获异常,并将stacktrace作为异常信息输出。可以使用`backtrace`和`backtrace_symbols`等函数获取函数调用栈的信息,并将其打印出来。 3. 自定义实现:也可以自己编写代码来实现获取stacktrace的功能。使用函数指针的方式,将每个函数的调用关系保存在一个自定义的数据结构中,并在需要时打印出来。 无论使用哪种方法,获取stacktrace可以帮助开发人员更快地定位问题发生的位置,从而快速解决bug。在生产环境中,由于性能和安全等方面的考虑,通常需要做一些限制,避免泄露敏感信息。因此,在实际应用中需要根据具体需求来选择合适的方法来获取stacktrace
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黄健华Yeah

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

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

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

打赏作者

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

抵扣说明:

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

余额充值