A function try block is a particular syntax of the C++ try block that embraces a whole function. The main advantage of this syntax over a "regular" try block containing all the function body is that a function try block applied over a constructor includes the constructor initializer list, and thus can catch the exceptions generated by the constructors of base classes and member objects.
Questions tagged [function-try-block]
15 questions
50
votes
6 answers
When is a function try block useful?
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 <<…

Nawaz
- 353,942
- 115
- 666
- 851
43
votes
4 answers
Difference between try-catch syntax for function
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…

iammilind
- 68,093
- 33
- 169
- 336
9
votes
2 answers
What is the purpose of a function try block?
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…

Belloc
- 6,318
- 3
- 22
- 52
6
votes
2 answers
Is it possible to have a function-try-block per member initialiser?
During the member initialisation of a class with multiple members, it seems desirable to be able to catch an exception generated by any specific member initialiser, to wrap in additional context for rethrowing, but the syntax of a function-try-block…

Adam Barnes
- 2,922
- 21
- 27
6
votes
2 answers
How to catch exception from member destructor
I wonder whether (and how) it's possible to catch an exception thrown in a member destructor. Example:
#include
class A
{
public:
~A() {
throw std::exception("I give up!");
}
};
class B
{
A _a;
public:
~B() {
…

Lukáš Bednařík
- 2,578
- 2
- 15
- 30
5
votes
2 answers
Weird "candidate expects 1 argument, 0 provided" in constructor
I'm making a simple threaded server application in C++, thing is, I use libconfig++ to parse my configuration files. Well, libconfig doesn't support multithreading, thus I'm using two wrapper classes in order to accomplish "support". Point is, one…

Misguided
- 1,302
- 1
- 14
- 22
3
votes
5 answers
Need for try catch within a constructor
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…

koobi
- 181
- 1
- 6
3
votes
1 answer
Should function-try-block of d'tor allow handling of throwing member-variable d'tor?
I have a class whose destructor is noexcept(false). I know it only throws under certain circumstances, and I want to use it as a member variable of a class with a noexcept destructor. From…

Ben
- 9,184
- 1
- 43
- 56
2
votes
3 answers
Does function try block allows us to resolve a exception?
So I was reading about function try block in this link. And there was a line that describes the difference between normal try block and function try block like this
unlike normal catch blocks, which allow you to either resolve an exception, throw a…

cuong.pq
- 137
- 1
- 1
- 4
2
votes
2 answers
Do function try blocks on non-contructor functions have any disadvantage?
Function try blocks are a special form of function bodies, for example:
int f() try {
// function body
}
catch {
// one or more catch-clauses.
}
The primary purpose is for usage in constructors, in order to log exceptions thrown by the…

ralfg
- 552
- 2
- 11
1
vote
3 answers
According to a knowledgeable author within the C++ community, the code shown below should not compile. Is he wrong?
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…

Belloc
- 6,318
- 3
- 22
- 52
0
votes
1 answer
Is calling a class member function which does some clean-up stuff on member variables safe if constructor throws?
I have the below code snippet which is to demonstrate use-case of function-try-block for constructor. Because the constructor of Controller throws no object of it gets created successfully, i.e. the client_ member variable should no longer exist too…

duong_dajgja
- 4,196
- 1
- 38
- 65
0
votes
1 answer
Stop try block automatically iterating to the next loop (for loop java)
Greetings to fellow stack overflowers viewing this question, I am an amateur working on a boat racing game in Java, currently facing a problem where the try block in a for loop immediately iterates to the next loop even the input is invalid. The for…

Han
- 33
- 6
0
votes
1 answer
in this example, what is the third missing possibility for the try block?
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++) {
…
user5849987
-1
votes
1 answer
What is thre reason of core dump?
Why do I have core dump in this code
I see output
Catch
terminate called after throwing an instance of 'std::runtime_error'
what(): ZERO DIV
Aborted (core dumped)
But I expect
Catch
terminate called after throwing an instance of…

mascai
- 1,373
- 1
- 9
- 30