简体   繁体   English

尝试接球中的不同接球

[英]Different catch in try-catch

What is difference between these types of catch, except that in first I can use e ? 这些捕获类型之间有什么区别,除了首先可以使用e

catch (Exception e)
{
   //some code;
}

catch (Exception)
{
   //some code;
} 

catch
{
   //some code;
} 

Catch can catch different exception's types. 捕获可以捕获不同异常的类型。
When you use the syntax catch(Exception) you are telling the compiler to write code that catches any kind of exceptions while, if you use a syntax like catch(InvalidOperationException) , you are asking to catch a specific type of exception 当您使用语法catch(Exception)时 ,是在告诉编译器编写捕获任何类型的异常的代码,而如果您使用类似catch(InvalidOperationException)的语法,则要求捕获特定类型的异常。

To simplify things you can write catch without any type and this has the same meaning of catch(Exception) 为了简化操作,您可以编写不带任何类型的catch ,这与catch(Exception)的含义相同

try
{
    // Uncomment this line to catch the generic exception
    // throw new Exception("An exception occurred");

    throw new InvalidOperationException("Operation x is not valid in this context");
}
// Comment the following lines to fall into the generic catch exception
catch (InvalidOperationException)
{
    // But without the variable we cannot print out the message....
    Console.WriteLine("An invalid operation has been catched");
}
catch (Exception)
{
    Console.WriteLine("An exception raised");
}

You cannot use the syntax catch(Exception ex) in the same try catch where you don't specify the name of the variable for the same type of exception. 您不能在未为相同类型的异常指定变量名称的同一个try catch中使用语法catch(Exception ex)

catch (Exception ex)
{
    Console.WriteLine(ex.Message);        
}
// Syntax error: CS0160: A previous catch clause already catches ......
catch (Exception)
{
    Console.WriteLine("An exception raised");
}

Strangely enough this doesn't result in a syntax error, but in a simple warning 足够奇怪的是,这不会导致语法错误,但是会产生一个简单的警告

catch(Exception)
{
   ....
}
// Warning CS1058: A previous catch clause already catches ......
catch
{
   ....
}

Of course you shouldn't catch exceptions that you are not prepared to handle. 当然,您不应该捕获不准备处理的异常。 If you do it just to expose a message you risk the correct functionality of your program. 如果这样做只是为了暴露信息,则可能会冒用程序正确功能的风险。 Usually you catch only specific exceptions that you are know how to handle to allow your program to continue. 通常,您只捕获特定的异常,您知道该如何处理以允许程序继续运行。 The only reason that I could find to catch all exceptions is when you write down the exception data in some kind of log file and then throw again the exception. 我能够捕获所有异常的唯一原因是,当您在某种日志文件中记录了异常数据,然后再次抛出该异常时。

catch(Exception ex)
{
    Logger.Error("Unexpected exception", ex);
    throw;   // NEVER throw ex;
}

Remember that it is really never required to write throw ex because you loose the stack trace of the exception and make very difficult to track down the exact error point. 请记住,实际上从来不需要写throw ex,因为这样会松散异常的堆栈跟踪,并且很难精确地找到错误点。

See: Best practices for catching and re-throwing .NET exceptions 请参阅: 捕获和重新抛出.NET异常的最佳实践

try{
    //do something
}catch{
    //do something
}

This catch is executed, regardless of the exception. 无论异常如何,都会执行此catch。

try{
    //do something
}catch (Exception) {
    //do something
}

This catch is executed when a specific Exception is thrown 抛出特定异常时执行此catch

try{
    //do something
}catch (Exception e) {
    //do something
}

Same here, only that you have a reference to the Exception. 此处相同,只是您对异常的引用。 That way, you have access to it. 这样,您就可以访问它。

Read more here . 在这里阅读更多。

If your code throws an exception, then the catch Block will be thrown and you have access to it over e . 如果您的代码抛出异常,那么catch块将被抛出,您可以通过e访问它。

catch (Exception e)
{
   //some code;
}

If your code throws an exception, then the catch Block will be thrown indepented from the exception type and you don't have access to it. 如果您的代码引发异常,那么catch块将独立于异常类型而抛出,您无权访问它。

catch
{
   //some code;
} 

If your code throws an exception, then the catch Block will be thrown depending from the exception type and you don't have access to it. 如果您的代码引发异常,则将根据异常类型引发catch块,并且您无权访问它。

catch (Exception)
{
   //some code;
} 

Instead of Exception you should use a more specific exception type! 您应该使用更具体的异常类型来代替Exception

let's check 让我们检查

in this code you can write e.Message for check Catch Message 在此代码中,您可以编写e.Message来检查捕获消息

catch (Exception e)
{
Console.WriteLine("Error Message is : " + e.Message);
}

but in this you just skip From Exception (All Exceptions) and you can add more Exceptions 但是在此您只需跳过From Exception(All Exceptions),就可以添加更多的例外

    catch (sqlExcetion)
    {
    //if your code have sqlEsception Get here 
    }
    catch (Exception)
    {
    //if your code have any Exception Get here
    } 

and in this code you can create one catch and all catch go this 在这段代码中,您可以创建一个catch,所有catch都可以

catch
{
   //all catch get here
} 

The minor difference between: 之间的微小区别:

try{
    //do something
}catch (Exception) {
    //do something
}

and

try{
    //do something
}catch (Exception e) {
    //do something
}

is: (the second one will give) 是:(第二个将给出)

The variable 'e' is declared but never used 声明了变量“ e”,但从未使用过

Also, if the code is like this: 另外,如果代码是这样的:

catch(Exception e) { throw e; }

the original stacktrace is gone. 原来的堆栈跟踪消失了。 So, you have to do: catch(Exception e) { throw; } 因此,您必须执行以下操作: catch(Exception e) { throw; } catch(Exception e) { throw; } to see the original stacktrace. catch(Exception e) { throw; }看到原来的堆栈跟踪。

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

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