MCPD 70536题目 获取异常信息

  You are working on a debug build of an application. You need to find the line of code that caused an exception to be thrown. Which property of the Exception class should you use to achieve this goal?

你正调试应用程序。你需要发现导致抛出异常代码的行。你使用哪个异常类的属性达到这个目标?

  AData

  BMessage

  CStackTrace

  DSource

 

  答案:C

  说明:

                   Data  获取一个提供用户定义的其他异常信息的键/值对的集合。
                   HelpLink  获取或设置指向此异常所关联帮助文件的链接。
                   HResult  获取或设置 HRESULT,它是分配给特定异常的编码数值。
                   InnerException  获取导致当前异常的 Exception 实例。
                   Message  获取描述当前异常的消息。
                   Source  获取或设置导致错误的应用程序或对象的名称。
                   StackTrace  获取当前异常发生时调用堆栈上的帧的字符串表示形式。执行堆栈跟踪在给定时刻正在执行的所有方法。对方法调用的跟踪称为堆栈跟踪。堆栈跟踪列表提供了一种循着调用序列跟踪到方法中异常发生处行号的手段。
                   TargetSite  获取引发当前异常的方法。

  例子:

    新建一个控制台程序。通过try catch获取异常。

class Program
    {
        
static void Main(string[] args)
        {
            
try
            {
                
//遍历一个空的DataTable,以便于抛出异常
                DataTable dtVal = null;
                
foreach (DataRow rowItem in dtVal.Rows)
                { }
            }
            
catch (Exception ex)
            {
                
//得到异常信息
                string errorTrace = ex.StackTrace;
                
string errorMessage = ex.Message;
                
string errorSource = ex.Source;
                
throw ex;
            }
        }
原文地址:https://www.cnblogs.com/scottckt/p/1847426.html