function-try-block

Need for try catch within a constructor

淺唱寂寞╮ 提交于 2020-01-02 07:51:15
问题 The link http://gotw.ca/gotw/066.htm states that Moral #1: Constructor function-try-block handlers have only one purpose -- to translate an exception. (And maybe to do logging or some other side effects.) They are not useful for any other purpose. While http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.8 If a constructor throws an exception, the object's destructor is not run. If your object has already done something that needs to be undone (such as allocating some memory, opening

What is the purpose of a function try block? [duplicate]

試著忘記壹切 提交于 2020-01-01 08:49:32
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When is a function try block useful? Difference between try-catch syntax for function This code throws an int exception while constructing the Dog object inside class UseResources . The int exception is caught by a normal try-catch block and the code outputs : Cat() Dog() ~Cat() Inside handler #include <iostream> using namespace std; class Cat { public: Cat() { cout << "Cat()" << endl; } ~Cat() { cout << "~Cat()

According to a knowledgeable author within the C++ community, the code shown below should not compile. Is he wrong?

微笑、不失礼 提交于 2019-12-24 17:52:46
问题 According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from where I've extracted the following text, regarding function-try-blocks : Toward Some Morals Incidentally, this also means that the only (repeat only) possible use for a constructor function-try-block is to translate an exception thrown from a base or member subobject. That's Moral #1. Next, Moral #2 says that destructor function-try-blocks are entirely usele-- "--But wait!" I hear

According to a knowledgeable author within the C++ community, the code shown below should not compile. Is he wrong?

这一生的挚爱 提交于 2019-12-24 17:51:04
问题 According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from where I've extracted the following text, regarding function-try-blocks : Toward Some Morals Incidentally, this also means that the only (repeat only) possible use for a constructor function-try-block is to translate an exception thrown from a base or member subobject. That's Moral #1. Next, Moral #2 says that destructor function-try-blocks are entirely usele-- "--But wait!" I hear

How to catch exception from member destructor

ⅰ亾dé卋堺 提交于 2019-12-06 02:26:29
问题 I wonder whether (and how) it's possible to catch an exception thrown in a member destructor. Example: #include <exception> class A { public: ~A() { throw std::exception("I give up!"); } }; class B { A _a; public: ~B() { // How to catch exceptions from member destructors? } }; 回答1: Yes, you can catch such an exception, using the function-try-block: class B { A _a; public: ~B() try { // destructor body } catch (const std::exception& e) { // do (limited) stuff } }; However, you cannot really do

When is a function try block useful?

最后都变了- 提交于 2019-11-27 04:35:32
I'm wondering when programmers use function try blocks. When is it useful? void f(int i) try { if ( i < 0 ) throw "less than zero"; std::cout << "greater than zero" << std::endl; } catch(const char* e) { std::cout << e << std::endl; } int main() { f(1); f(-1); return 0; } Output: (at ideone ) greater than zero less than zero EDIT: As some people might think that the syntax of function defintion is incorrect (because the syntax doesn't look familiar), I've to say that no its not incorrect. Its called function-try-block. See §8.4/1 [dcl.fct.def] in the C++ Standard. You use it in constructors to

Difference between try-catch syntax for function

巧了我就是萌 提交于 2019-11-26 16:01:53
I came across this syntax recently for try-catch for function. struct A { int a; A (int i) : a(i) // normal syntax { try {} catch(...) {} } A () // something different try : a(0) {} catch(...) {} void foo () // normal function try {} catch(...) {} }; Both syntax are valid . Is there any technical difference between these syntax apart from coding style ? Is one of the syntax superior to other by any aspect ? The First Syntax: The scope of the try block starts after the Member Initialization list has been completed, So any exception thrown during Member Initialization will not be caught by this

When is a function try block useful?

坚强是说给别人听的谎言 提交于 2019-11-26 11:16:00
问题 I\'m wondering when programmers use function try blocks. When is it useful? void f(int i) try { if ( i < 0 ) throw \"less than zero\"; std::cout << \"greater than zero\" << std::endl; } catch(const char* e) { std::cout << e << std::endl; } int main() { f(1); f(-1); return 0; } Output: (at ideone) greater than zero less than zero EDIT: As some people might think that the syntax of function defintion is incorrect (because the syntax doesn\'t look familiar), I\'ve to say that no its not

Difference between try-catch syntax for function

落爺英雄遲暮 提交于 2019-11-26 04:40:22
问题 I came across this syntax recently for try-catch for function. struct A { int a; A (int i) : a(i) // normal syntax { try {} catch(...) {} } A () // something different try : a(0) {} catch(...) {} void foo () // normal function try {} catch(...) {} }; Both syntax are valid. Is there any technical difference between these syntax apart from coding style ? Is one of the syntax superior to other by any aspect ? 回答1: The First Syntax: The scope of the try block starts after the Member