有没有办法从捕获的异常对象中提取函数调用栈中各个函数的实参???
如以下程序,我想在testfun()中从 e 中得到 fun 的两个实参 2 和 3, 
以及Substring 的 1 和 3, 不知能否得到???
如能,怎样得到??? private void testfun()
{  
try{
fun(2, 3);
}
catch(Exception e){ string a = e.Message.ToString();
}
} private void fun(int i, int k)
{
string a1 = "";
string c1 = a1.Substring(1,3);
}

解决方案 »

  1.   

    估计没戏,如果用AOP倒是可以,不过太慢了
      

  2.   

    你可以自己去继承Exception,然后在
    private void fun(int i, int k)
    {
    string a1 = "";
    string c1 = a1.Substring(1,3);
    }
    中先捕获异常,然后转换成自己定义的异常对象,然后再throw
    这样你在外边的只捕获你定义的异常类型即可
      

  3.   

    sample as follows
    public class MyException:Exception
    {
    private string strAppendMessage = "";
    public string AppendMessage
    {
    get{ return strAppendMessage;}
    set{ strAppendMessage = value;}
    }
    public MyException( ):base()
    {
    }
    } private void fun(int i, int k)
    {
    try
    {
    string a1 = "";
    string c1 = a1.Substring(1,3);
    }
    catch( Exception err )
    {
    MyException myErr = new MyException();
    myErr.AppendMessage = string.Format( 
    "Error Message:{2}\nFirst Par(int):{0}\nSecondPar(int):{1};",
    i,k, err.Message );
    throw myErr;
    }
    }
    //Calling
    try
    {
    fun( 1, 2 );
    }
    catch( MyException myErr )
    {
    MessageBox.Show( myErr.AppendMessage );
    }
      

  4.   

    to : Knight94(愚翁) 你的方法不错,但是,有几千个调用者函数呢,挨个改改不及阿,只能用统一方法。
      

  5.   

    Sorry
    刚才说错了
    private void fun(int i, int k)
    {
    try
    {
    string a1 = "";
    string c1 = a1.Substring(1,3);
    }
    catch( Exception err )
    {
    string strPara = string.Format( "First Par(int):{0}\nSecondPar(int):{1};", i, k );
    ApplicationException myErr = new ApplicationException( strPara + “\n” + err.Message, err );
    throw myErr;
    }
    } 然后在捕获的时候,如下即可:
    try
    {
    fun( 1, 2 );
    }
    catch( Exception myErr )
    {
    MessageBox.Show( myErr.Message );
    }
      

  6.   

    to : Knight94(愚翁) 不好意思,你的方法比较正点,但我想找的是通用的方法,
    使用系统调用的,直接从系统中得到参数列表,不想
    每个函数都要写上不同的语句。