分隔try块的范围? [英] Separating scope of try block?

查看:73
本文介绍了分隔try块的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法(成语,构造,类似的东西)从代码区域离开一个try块的

范围,它将捕获

例外?我的意思是,假设我有这样的代码片段:


try {

Object o(data,25,16384);

o.process(); //这可能会抛出,但我不想在这里处理它。

} catch(...){

std :: cerr<< 无法构造对象 << std :: endl;

}


现在,假设o的构造可能会因异常而失败,这就是为什么
是为什么我们把它放在一个try块中。如果发生这种情况,我们想要做一些事情来处理它(在这种情况下,通知用户并继续前进)。但是在它构建之后,我们想要用对象做一些其他事情,

这些东西也可能会失败。但如果确实如此,我们希望异常

向外传播给周围的处理程序。


有没有办法从构造函数中捕获异常而不是陷阱

后续处理对象的例外情况?

Is there a way (idiom, construct, anything like that) to divorce the
scope of a try block from the region of code where it will catch the
exception? I mean, suppose I have a code snippet like this:

try {
Object o("data", 25, 16384);
o.process(); // This may throw, but I don''t want to handle it here.
} catch (...) {
std::cerr << "Couldn''t construct the object" << std::endl;
}

Now, suppose the construction of o could fail with an exception, which
is why we put it inside a try block. If this happens, we want to do
something to handle it (in this case, notify the user and move on). But
after it''s constructed, we want to do something else with the object,
and this stuff could also fail. But if it does, we want the exception
to propagate outward to the surrounding handlers.

Is there a way to trap exceptions from a constructor and not trap
exceptions in the subsequent processing of the object?

推荐答案

Adam H. Peterson写道:
Adam H. Peterson wrote:
有没有办法(成语,构造,类似的东西)将try块的范围与代码区域分开,它将捕获
异常?我的意思是,假设我有一个这样的代码片段:

尝试{
对象o(数据,25,16384);
o.process(); //这可能会抛出,但我不想在这里处理它。
} catch(...){
std :: cerr<< 无法构造对象 << std :: endl;
}
现在,假设o的构造可能会因异常而失败,这就是为什么我们把它放在try块中。如果发生这种情况,我们希望做一些事情来处理它(在这种情况下,通知用户并继续前进)。但是在构建之后,我们想要用对象做其他事情,
这些东西也可能会失败。但是如果确实如此,我们希望异常
向外传播给周围的处理程序。

有没有办法从构造函数中捕获异常而不是陷阱
后续异常处理对象?
Is there a way (idiom, construct, anything like that) to divorce the
scope of a try block from the region of code where it will catch the
exception? I mean, suppose I have a code snippet like this:

try {
Object o("data", 25, 16384);
o.process(); // This may throw, but I don''t want to handle it here.
} catch (...) {
std::cerr << "Couldn''t construct the object" << std::endl;
}

Now, suppose the construction of o could fail with an exception, which
is why we put it inside a try block. If this happens, we want to do
something to handle it (in this case, notify the user and move on). But
after it''s constructed, we want to do something else with the object,
and this stuff could also fail. But if it does, we want the exception
to propagate outward to the surrounding handlers.

Is there a way to trap exceptions from a constructor and not trap
exceptions in the subsequent processing of the object?




抛出一个不同的例外。


-

Ian Collins 。



Throw a different exception.

--
Ian Collins.


Adam H. Peterson写道:


bool oIsConstructed = false;
Adam H. Peterson wrote:

bool oIsConstructed = false;
尝试{
对象o(数据,25,16384);


oIsConstructed = true;

o.process(); //这可能会抛出,但我不想在这里处理它。
} catch(...){


总是按名称捕获。 ...是一个只对调试有用的黑客。


if(oIsConstructed)throw;

std :: cerr<< 无法构造对象 << std :: endl;
}
try {
Object o("data", 25, 16384);
oIsConstructed = true;
o.process(); // This may throw, but I don''t want to handle it here.
} catch (...) {
Always catch by name. ... is a hack that''s only useful for debugging.

if (oIsConstructed) throw;
std::cerr << "Couldn''t construct the object" << std::endl;
}




你在盒子里想的太远了。尝试捕获是有用的,因为我们的原始老式技术仍然有效。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



You were thinking too far inside the box. try catch is useful because our
original old-fashioned techniques still also work.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Ian Collins写道:
Ian Collins wrote:
抛出不同的例外。



并注意我的帖子也没想到那个盒子外面。


只有在你不能将源代码更改为Object或者它的

构造函数很深,你不知道它会抛出什么。


或者,在构造函数内部放置一个try-catch块

(这是一种特殊的语法),抓住任何它抛出的东西,并重新抛出

作为Ian的特殊例外。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



And note my post didn''t think outside that box, either.

Use my technique only if you can''t change the source to Object, or if its
constructor is deep and you don''t know what it will throw.

Alternately, put a try-catch block around the inside of the constructor
(there''s a special syntax for that), catch whatever it throws, and rethrow
as Ian''s special exception.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


这篇关于分隔try块的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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