尝试捕获 - 但抓住了一切 [英] try-catch - but a catch which catches everything

查看:58
本文介绍了尝试捕获 - 但抓住了一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有这样的事情:


试试

{

//一些代码

}

catch // note - 我现在正在捕捉所有内容

{

//做点什么

}


请问这种捕获语句,捕获''不安全''和''核心级''
例外(即使应用程序是一个简单的asp.net应用程序,没有不安全的东西)?

(只是想确认我和我的一个朋友辩论 - AFAIK我

觉得它不是因为没有使用不安全的代码,因为Win32异常是

映射到托管.NET异常类)

另外,在很多情况下,我有一些没有任何作用的块 - 因为我不想要任何例外来传播......就像下面这样。


public bool IsInteger(string val)

{

bool result;


try

{

int.Parse(val);

result = true;

}

catch

{

result = false;

}

返回结果;

}


这样可以(一个好的做法)?


谢谢,

Benny

解决方案

Benny,


如果你使用了catch而没有指定异常,那么只要在try
代码>
块。现在,如果运行时没有映射的SEH异常没有映射,或者以某种方式逃避它,那么整个过程可能会超过

无论如何,这是一个有争议的问题。


你也问以下是不是好的做法:

public bool IsInteger(string val)
{
bool结果;

尝试
{
int.Parse(val);
result = true;
}
catch
{
结果=假;
}
返回结果;
}


这个很可怕实际上,您应该使用静态TryParse方法

来查看它是否可以被解析,或者自己执行一些解析。依赖

任何抛出的异常都是一个很糟糕的指标,表明问题确实是什么。例如,你应该捕获ArgumentException(如果参数是

null,那么它不能是一个整数),并且FormatException(字符串不是

an整数)和可能的OverflowException(它是一个整数,但不能是存储在Int32中的
,你如何处理这个取决于你)。


如果你得到一个OutOfMemoryException,那么这与你的代码没什么关系,你不应该吞下它,因为堆栈中的代码可能需要

知道这是在继续。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

"尼" <成为*** @ discussions.microsoft.com>在留言中写道

新闻:F4 ********************************** @ microsof t.com ...

我有这样的事情:

尝试
{
//一些代码
} <抓住//注意 - 我现在正抓住一切
{
//做点什么


请问这种抓住声明,抓住''不安全''和''核心级''
例外(即使应用程序是一个简单的asp.net应用程序,没有不安全的东西)?
(只是想确认,因为我与其中一个我的朋友 - AFAIK我感觉它不会因为没有使用不安全的代码而且因为Win32异常
被映射到托管的.NET异常类中)

此外,在许多情况下,我有一些无效的捕获块 - 因为我不希望任何异常被传播......如下所示。

公共bool IsInteger(字符串val)
{
bool结果;

尝试
{
int.Parse(val);
结果=真;
}
抓住
{
结果=假;
}
返回结果;
}

谢谢,
本尼



嗨尼古拉斯,

如果您使用catch而不指定异常,则只要在try
中抛出任何托管异常,就会调用该部分的代码。

什么是SEH?

另外,我的问题实际上是 - 是否添加catch(Exception ex)使

确保它捕获*仅托管*异常。换句话说,如果我省略

(Exception ex),catch块是否会捕获不安全和内核级异常

(即使没有任何不安全的代码直接)?


这太可怕了。实际上,您应该使用静态TryParse方法
来查看它是否可以被解析,或者自己执行一些解析。依赖




只有Double结构有一个TryParse方法 - 一个int没有(我用的是

..NET 1.1) - 你指的是其他方法吗?


我的目标是确保实用程序方法是故障安全的。在其他

字中,无论发生什么错误,该方法都必须返回false。如果每个人都好b / b
,它必须返回true。这是为了确保调用方法需要

而不用担心异常 - 这些只是实用程序类。


请分享您的想法。


谢谢,

Benny


Benny,


SEH是结构化异常处理的缩写,它是一种在非托管代码中抛出/处理异常的方式。如果CLR选择了这个,那么它将会产生一个托管异常,其中包含SEH的详细信息。


" catch"只会捕获托管异常。它没有别的东西能够抓住它。如果CLR接收到一个非托管异常(它知道要查找
),那么它会将其包装在托管异常中并抛出它。

如果它逃脱CLR'的手表,然后这个过程很可能会下降。


捕获和捕获之间的唯一微小区别(例外e)是

与捕获单独,你可以抓住任何东西,而不仅仅是例外。你可以实际抛出从托管C ++中的对象(即任何东西)派生的任何东西,但是你不能用C#来做。使用catch(例外e),你只会获得从异常中得到的任何东西(在之前的捕获中没有处理过

块)。


catch(它本身就是)的东西是你无法访问

抛出的对象。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Benny" <成为*** @ discussions.microsoft.com>在留言中写道

新闻:E4 ********************************** @ microsof t.com ...

嗨尼古拉斯,

如果你使用了一个catch而没有指定例外,那么
部分尝试

时会调用代码>



什么是SEH?
另外,我的问题实际上是 - 是否添加catch (ex ex)使
确保它只捕获*托管*例外。换句话说,如果我省略
(Exception ex),catch块是否会捕获不安全和内核级别的异常
(即使没有任何不安全的代码)? br />

这太可怕了。实际上,您应该使用静态TryParse
方法来查看它是否可以被解析,或者自己执行一些解析。依赖



只有Double结构有一个TryParse方法 - 一个int没有(我使用
.NET 1.1) - 你指的是其他方法吗?

我的目标是确保实用方法是故障安全的。在
其他单词中,无论发生什么错误,该方法都必须返回false。如果每个人都好,那就必须归还真实。这是为了确保调用方法
不需要担心异常 - 这些只是实用程序类。

请分享您的想法。

谢谢,
本尼



Hi,

I have something like this:

try
{
// some code
}
catch // note - i am catching everything now
{
// do something
}

Will this sort of catch statement, catch ''unsafe'' and ''kernal-level''
exceptions (even if the app is a simple asp.net app with no unsafe stuff)?
(just wanted to confirm as I had a debate with one of my friends - AFAIK i
feel it doesn''t since no unsafe code is used and since Win32 exceptions are
mapped into managed .NET exception classes)
Also, in many cases, i have catch blocks which does nothing - because i
don''t want any exception to be propagated...something like follows.

public bool IsInteger(string val)
{
bool result;

try
{
int.Parse(val);
result = true;
}
catch
{
result = false;
}
return result;
}

Is this OK (a good practice)?

Thanks,
Benny

解决方案

Benny,

If you use a catch, without specifying the exception, then that section
of code will be called whenever ANY managed exception is thrown in the try
block. Now, if there are SEH exceptions that are thrown that are not mapped
by the runtime, or somehow escape it, then the whole process is more than
likely going to come down, and it''s a moot point anyways.

You also ask if the following is good practice:

public bool IsInteger(string val)
{
bool result;

try
{
int.Parse(val);
result = true;
}
catch
{
result = false;
}
return result;
}
This is horrible. In reality, you should use the static TryParse method
to see if it can be parsed, or perform some parsing on your own. Relying on
any exception to be thrown is a poor indicator of what the problem really
is. You should, for example, catch ArgumentException (if the argument is
null, then it can''t be an integer), and FormatException (the string is not
an integer) and possibly OverflowException (it is an integer, but can not be
stored in an Int32, how you handle this is up to you).

If you get an OutOfMemoryException, then that has nothing to do with
your code, and you shouldn''t just swallow it, as code up the stack might
need to know this is going on.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Benny" <Be***@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com... Hi,

I have something like this:

try
{
// some code
}
catch // note - i am catching everything now
{
// do something
}

Will this sort of catch statement, catch ''unsafe'' and ''kernal-level''
exceptions (even if the app is a simple asp.net app with no unsafe stuff)?
(just wanted to confirm as I had a debate with one of my friends - AFAIK i
feel it doesn''t since no unsafe code is used and since Win32 exceptions
are
mapped into managed .NET exception classes)
Also, in many cases, i have catch blocks which does nothing - because i
don''t want any exception to be propagated...something like follows.

public bool IsInteger(string val)
{
bool result;

try
{
int.Parse(val);
result = true;
}
catch
{
result = false;
}
return result;
}

Is this OK (a good practice)?

Thanks,
Benny



Hi Nicholas,

If you use a catch, without specifying the exception, then that section
of code will be called whenever ANY managed exception is thrown in the try
What is SEH?
Also, my question actually is - whether to add catch(Exception ex) to make
sure that it catches *only managed* exceptions. In other words, if I omit
(Exception ex), will the catch block catch unsafe and kernel level exceptions
too (even if there isn''t any unsafe code directly)?

This is horrible. In reality, you should use the static TryParse method
to see if it can be parsed, or perform some parsing on your own. Relying on



Only the Double structure has a TryParse method - an int doesn''t (I am using
..NET 1.1) - were you referring to some other method?

My objective is to make sure that the utility method is fail-safe. In other
words, whatever error happens, the method must return a false. If everythings
fine, it must return a true. This is to ensure that the calling method need
not worry about exceptions - these are just utility classes.

Please share your thoughts.

Thanks,
Benny


Benny,

SEH is short for Structured Exception Handling, which is a way of
throwing/handling exceptions in unmanaged code. If the CLR picks up on
this, it is going to throw a managed exception with the details of the SEH.

"catch" will only catch managed exceptions. There is nothing else that
it can catch. If the CLR picks up on an unmanaged exception (that it knows
to look for), then it will wrap that in a managed exception and throw that.
If it escapes the CLR''s watch, then the process is most likely coming down.

The only slight difference between catch and catch(Exception e) is that
with "catch" alone, you can catch anything, not just exceptions. You can
actually throw anything derived from object (i.e. anything) in managed C++,
but you can not do it in C#. With catch(Exception e), you will only catch
anything deriving from Exception (that isn''t handled in previous catch
blocks).

The thing with catch (on it''s own) is that you can''t access anything on
the object that is thrown.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Benny" <Be***@discussions.microsoft.com> wrote in message
news:E4**********************************@microsof t.com...

Hi Nicholas,

If you use a catch, without specifying the exception, then that
section
of code will be called whenever ANY managed exception is thrown in the
try



What is SEH?
Also, my question actually is - whether to add catch(Exception ex) to make
sure that it catches *only managed* exceptions. In other words, if I omit
(Exception ex), will the catch block catch unsafe and kernel level
exceptions
too (even if there isn''t any unsafe code directly)?

This is horrible. In reality, you should use the static TryParse
method
to see if it can be parsed, or perform some parsing on your own. Relying
on



Only the Double structure has a TryParse method - an int doesn''t (I am
using
.NET 1.1) - were you referring to some other method?

My objective is to make sure that the utility method is fail-safe. In
other
words, whatever error happens, the method must return a false. If
everythings
fine, it must return a true. This is to ensure that the calling method
need
not worry about exceptions - these are just utility classes.

Please share your thoughts.

Thanks,
Benny



这篇关于尝试捕获 - 但抓住了一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆