Skip to main content
 首页 » 编程设计

c#之内部异常 C#

2024年02月13日12oomusou

故事: 我有来自 3 个不同类的 3 个函数。函数调用顺序是:

Form1_Load(...) -> Student.GetAllStudents(...) -> StudentDAL.GetStudentInformation(...) -> ConnectionManager.GetConnection(...)

我想做的是在 中显示最内层函数的 StackTrace,即 ConnectionManager.GetConnection() Form1 类 中的 MessageBox。换句话说,我不想在任何内部类中使用 MessageBox,而只在最外层的类 Form1 类 中使用。

问题: 要获取内部异常,我们可以使用 InnerException 或 GetBaseException() 等,但是当我try to get inner exception 它抛出一个异常“对象引用未设置到一个实例”,这意味着没有内部异常,当我检查时,该值也是 null。我只想知道为什么它是 null?它不应该引用内部异常吗?如果我错了,请纠正我。

函数代码:

  1. Form1_Load(...)

    private void Form1_Load(object sender, EventArgs e) 
          { 
               try 
               { 
                    DataTable dt = new DataTable(); 
                    dt.Load((**new Student().GetAllStudents()**)); 
                    if (dt.Rows.Count <= 0) 
                    { 
                         MessageBox.Show("Student table empty."); 
                    } 
                    else 
                    { 
                         this.dataGridView1.DataSource = dt; 
                    } 
               } 
               catch (Exception ex) 
               { 
                    MessageBox.Show(ex.Message+Environment.NewLine+"Source(s) : "+ex.StackTrace.Substring(0, ex.StackTrace.LastIndexOf("at"))); 
               } 
    
  2. GetAllStudents(...)

    public SqlDataReader GetAllStudents() 
          { 
               try 
               { 
                    return StudentInformationDataAccessLayer.GetStudentInformation(); 
               } 
               catch (Exception ex) 
               { 
                    throw ex; 
               } 
          } 
    
  3. GetStudentInformation(...)

    public static SqlDataReader GetStudentInformation() 
          { 
               try 
               { 
                    SqlConnection sqlCon = null; 
                    sqlCon = ConnectionManager.GetConnection(); 
                    if (sqlCon == null) 
                    { 
                         return null; 
                    } 
                    String Query = null; 
                    Query  = "SELECT * FROM [dbo].[Student]"; 
                    SqlCommand cmd = new SqlCommand(Query, sqlCon); 
                    SqlDataReader dr = null; 
                    dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
                    return dr; 
               } 
               catch (Exception ex) 
               { 
                    throw ex; 
               } 
          } 
    
  4. GetConnection(...)

    public static SqlConnection GetConnection() 
          { 
               String _connectionString = null; 
               _connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; 
     
               if (_connectionString == null) 
               { 
                    return null; 
               } 
     
               try 
               { 
                    SqlConnection connection = new SqlConnection(_connectionString); 
                    connection.Open(); 
                    return connection; 
               } 
               catch (Exception ex) 
               { 
                    throw ex; 
               } 
          } 
    

请您参考如下方法:

如果您希望保留堆栈跟踪和异常信息,您应该像这样更改重新抛出捕获的异常的代码:

 } 
 catch(Exception ex) 
 { 
     // do what you need to do with ex 
     // .. 
     // rethrow.. 
 
     throw;           // notice this is not "throw ex"; 
 } 

仅使用 throw; 重新抛出异常会保留原始堆栈跟踪。不一定会有内部异常,但这不是您应该关心的。您需要知道的是异常起源的堆栈跟踪。