C#-如何将(Exception-ex)公开

本文关键字:Exception-ex 公开 C#- | 更新日期: 2023-09-27 17:57:55

如何在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.

所以,如果前任是公开的,我可以试试这个:

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

C#-如何将(Exception-ex)公开

我不确定"execute ex"是什么意思,但这就是在catch块之后访问Exception的方法:

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

你做的事情很奇怪。停止。