有些帖子询问这两者之间的区别是什么.
(为什么我还要提这个...)
但我的问题是不同的,我称之为"抛出前"在另一个错误的神像处理方法.
public class Program {
public static void Main(string[] args) {
try {
// something
} catch (Exception ex) {
HandleException(ex);
}
}
private static void HandleException(Exception ex) {
if (ex is ThreadAbortException) {
// ignore then,
return;
}
if (ex is ArgumentOutOfRangeException) {
// Log then,
throw ex;
}
if (ex is InvalidOperationException) {
// Show message then,
throw ex;
}
// and so on.
}
}
Run Code Online (Sandbox Code Playgroud)
如果try & catch用于Main,那么我会throw; …
可能重复:
throw和throw之间的差异新异常()
有什么意义
catch (Exception)
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
这是做什么的?
C++之间的区别是什么?
try { /*some code here*/}
catch(MyException& ex)
{ throw ex;} //not just throw
Run Code Online (Sandbox Code Playgroud)
和
try { /*some code here*/}
catch(MyException& ex)
{ throw;} //not throw ex
Run Code Online (Sandbox Code Playgroud)
它只是在堆栈跟踪中(在C++中,在任何情况下都不是C#或Java中的标准)?
(如果它有任何区别,我使用MSVS 2008.)
考虑一下这种方法(原谅Chuck Norris幽默的悲惨尝试:)):
public class ChuckNorrisException : Exception
{
public ChuckNorrisException()
{
}
public ChuckNorrisException(string message)
: base(message)
{
}
public ChuckNorrisException(string message, Exception cause)
: base(message, cause)
{
}
protected ChuckNorrisException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
static void ExceptionTest(double x)
{
try
{
double y = 10 / x;
Console.WriteLine("quotient = " + y);
}
catch (Exception e)
{
e = e is DivideByZeroException ? new ChuckNorrisException("Only Chuck Norris can divide by 0!", e) :
e; …Run Code Online (Sandbox Code Playgroud)