简体   繁体   English

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

[英]C# - How to make (Exception ex) as public

How can I make ex accessible after a try catch block?如何在 try catch 块之后使 ex 可访问? Like this...像这样...

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

Why do I want to do this?我为什么要这样做? If I write:如果我写:

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.

So maybe if ex is public, I can try this:所以也许如果 ex 是公开的,我可以试试这个:

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

I am not sure what you mean with "execute ex", but this is how you could access the Exception after the catch block:我不确定“execute ex”是什么意思,但这是在catch块之后访问异常的方法:

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
}

The thing you are doing is weird.你做的事情很奇怪。 Stop it.停下来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM