C# - 如何将(Exception ex)公之于众

时间:2014-04-11 07:39:48

标签: c#

如何在try catch块之后使ex可访问? 像这样......

try
{
    // do something...
}
catch (Exception ex) {
    // skip here...
}
//execute **ex** here

为什么我要这样做? 如果我写:

try
{
    // do something...
    // i already declared x as public.
    x = "what ever";
}
catch (Exception ex) {
    // if there's an error...
    Console.WriteLine(ex);
}
// Even there's an error,
// there's still no output.

所以也许如果ex是公开的,我可以试试这个:

try
{
    // do something...
}
catch (Exception ex) {
    // skip here...
}
// execute **ex** here

2 个答案:

答案 0 :(得分:6)

我不确定您对“执行ex”的意思,但这是您在catch阻止后访问异常的方式:

Exception ex = null;
try
{
    // do something...
}
catch (Exception ex1) {
    ex = ex1;
}

if(ex != null)
   // ...

答案 1 :(得分:6)

Exception exceptionObject = null;

try
{
    // do something...
}
catch (Exception ex) {
    exceptionObject = ex;
}
// execute **ex** here
if(exceptionObject != null)
{
    //do a thing
}

你正在做的事情很奇怪。停下来。