有没有办法从捕获的异常对象中提取函数调用栈中各个函数的实参???
如以下程序,我想在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);
}
private void fun(int i, int k)
{
string a1 = "";
string c1 = a1.Substring(1,3);
}
中先捕获异常,然后转换成自己定义的异常对象,然后再throw
这样你在外边的只捕获你定义的异常类型即可
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 );
}
刚才说错了
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 );
}
使用系统调用的,直接从系统中得到参数列表,不想
每个函数都要写上不同的语句。