Topic: Try for Object-initialization
Author: abymaroth@googlemail.com
Date: Thu, 25 Sep 2014 12:27:59 -0700 (PDT)
Raw View
------=_Part_7650_267086377.1411673279017
Content-Type: text/plain; charset=UTF-8
The following pattern is often used in applications:
int main(int argc, char **argv)
{
SomeObject object;
while (running) // this is running for the whole application lifetime
(object must be valid the whole time)
{
// do something with object
}
}
Now I want to check if something goes wrong inside the initialization of
object. I would now embed the initialization of object into a try-block and
handle exceptions inside of a catch-block. But if I do so, the lifetime of
object is bound to the try-block which is not my intention. I could now use
a pointer and allocate/delete it at the right places but the only reason I
would do this here is because the try-catch-syntax forces me to.
Of course a normal try-catch without scope change won't work either as I
can ignore the exception in the catch-block and the object would not be
valid in case of an exception.
I thought of a try-expression for object initializations only which keeps
the current scope but will always leave the current scope in case of an
exception (after optional handling of the exception).
The catch-blocks for this have to:
- leave the current scope (of the object initialization) after executing
- forbid referencing the not-initialized object (could be checked by the
compiler easily)
My suggestion would look like this:
try SomeObject object
catch (const std::exception &e)
{
// this acts like a finally-block to clean-up stuff that was created
before the initialization of object
// and to handle the exception itself.
// the scope of "try SomeObject object" is left at the end of this
block.
}
Because the current scope could be a function that has a return type and
the return value would be undefined in this case, I have two suggestions
for this issue:
1. re-throw the exception out of the current scope or terminate the
application if the scope is the main entry point
2. the compiler forces a valid return-statement at the end of each of these
catch-blocks
This won't break RAII at all, as the scope is left if the object is not
created.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_7650_267086377.1411673279017
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">The following pattern is often used in applications:<br><b=
r><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250);=
border-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; =
word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpretty=
print"><span style=3D"color: #008;" class=3D"styled-by-prettify">int</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> main</span><span=
style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">int</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> argc</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">char</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
**</span><span style=3D"color: #000;" class=3D"styled-by-prettify">argv</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"><br> </span><span style=
=3D"color: #606;" class=3D"styled-by-prettify">SomeObject</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">object</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"><br><br> </span><span style=3D"color:=
#008;" class=3D"styled-by-prettify">while</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">running</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> // this is running for the whole application lifetime (object must be v=
alid the whole time)<br> </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br></span><code class=3D"prettyprint"><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify"></span></code><code class=3D=
"prettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettify">&nb=
sp; </span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>// do something with object</span></code><br><span style=3D"color: #000;" =
class=3D"styled-by-prettify"><code class=3D"prettyprint"><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"></span><span style=3D"color: #660;"=
class=3D"styled-by-prettify"></span></code> </span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span></div></code></div><br>Now I want to che=
ck if something goes wrong inside the initialization of object. I would now=
embed the initialization of object into a try-block and handle exceptions =
inside of a catch-block. But if I do so, the lifetime of object is bound to=
the try-block which is not my intention. I could now use a pointer and all=
ocate/delete it at the right places but the only reason I would do this her=
e is because the try-catch-syntax forces me to.<br><br>Of course a normal t=
ry-catch without scope change won't work either as I can ignore the excepti=
on in the catch-block and the object would not be valid in case of an excep=
tion.<br><br>I thought of a try-expression for object initializations only =
which keeps the current scope but will always leave the current scope in ca=
se of an exception (after optional handling of the exception).<br><br>The c=
atch-blocks for this have to:<br><br>- leave the current scope (of the obje=
ct initialization) after executing<br>- forbid referencing the not-initiali=
zed object (could be checked by the compiler easily)<br><br>My suggestion w=
ould look like this:<br><br><div class=3D"prettyprint" style=3D"background-=
color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: =
solid; border-width: 1px; word-wrap: break-word;"><code class=3D"prettyprin=
t"><div class=3D"subprettyprint"><span style=3D"color: #606;" class=3D"styl=
ed-by-prettify">try SomeObject object<br>catch (const std::exception &e=
)<br></span><span style=3D"color: #660;" class=3D"styled-by-prettify"></spa=
n>{<br> // this acts like a finally-block to clean-up stu=
ff that was created before the initialization of object<br> &nbs=
p; // and to handle the exception itself.<br> // the scop=
e of "<code class=3D"prettyprint"><span style=3D"color: #606;" class=3D"sty=
led-by-prettify">try SomeObject object" is left at the end of this</span></=
code> block.<br>}</div></code></div><br>Because the current scope could be =
a function that has a return type and the return value would be undefined i=
n this case, I have two suggestions for this issue:<br><br>1. re-throw the =
exception out of the current scope or terminate the application if the scop=
e is the main entry point<br>2. the compiler forces a valid return-statemen=
t at the end of each of these catch-blocks<br><br>This won't break RAII at =
all, as the scope is left if the object is not created.<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_7650_267086377.1411673279017--
.
Author: Magnus Fromreide <magfr@lysator.liu.se>
Date: Thu, 25 Sep 2014 23:48:36 +0200
Raw View
On Thu, Sep 25, 2014 at 12:27:59PM -0700, abymaroth@googlemail.com wrote:
> The following pattern is often used in applications:
>
> int main(int argc, char **argv)
> {
> SomeObject object;
>
> while (running) // this is running for the whole application lifetime
> (object must be valid the whole time)
> {
> // do something with object
> }
> }
>
> Now I want to check if something goes wrong inside the initialization of
> object. I would now embed the initialization of object into a try-block and
> handle exceptions inside of a catch-block. But if I do so, the lifetime of
> object is bound to the try-block which is not my intention. I could now use
> a pointer and allocate/delete it at the right places but the only reason I
> would do this here is because the try-catch-syntax forces me to.
Does the following bring you halfway there?
int main(int argc, char **argv)
{
struct X : SomeObject {
X() try : SomeObject() { } catch(...) { /* cleanup */ }
};
X object;
while (running) { /* do something with object */ }
}
> 1. re-throw the exception out of the current scope or terminate the
> application if the scope is the main entry point
> 2. the compiler forces a valid return-statement at the end of each of these
> catch-blocks
A drawback with my hack is that you can't return if the SomeObject constructor
throws due to the implicit rethrow at the end of the catch block.
/MF
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Andy Prowl <andy.prowl@gmail.com>
Date: Thu, 25 Sep 2014 15:10:43 -0700 (PDT)
Raw View
------=_Part_37_1476523460.1411683043531
Content-Type: text/plain; charset=UTF-8
Sorry, perhaps a stupid question, but if the object needs to be valid the
whole time while the application is running (this is what I understand from
the comment in your first code snippet), why can't you simply put the loop
inside the try/catch block as well?
int main(int argc, char **argv)
{
try
{
SomeObject object;
while (running) // this is running for the whole application
lifetime (object must be valid the whole time)
{
// do something with object
}
}
catch (std::exception const&)
{
// Whatever...
}
}
Cheers,
Andy
On Thursday, September 25, 2014 9:27:59 PM UTC+2, abym...@googlemail.com
wrote:
>
> The following pattern is often used in applications:
>
> int main(int argc, char **argv)
> {
> SomeObject object;
>
> while (running) // this is running for the whole application lifetime
> (object must be valid the whole time)
> {
> // do something with object
> }
> }
>
> Now I want to check if something goes wrong inside the initialization of
> object. I would now embed the initialization of object into a try-block and
> handle exceptions inside of a catch-block. But if I do so, the lifetime of
> object is bound to the try-block which is not my intention. I could now use
> a pointer and allocate/delete it at the right places but the only reason I
> would do this here is because the try-catch-syntax forces me to.
>
> Of course a normal try-catch without scope change won't work either as I
> can ignore the exception in the catch-block and the object would not be
> valid in case of an exception.
>
> I thought of a try-expression for object initializations only which keeps
> the current scope but will always leave the current scope in case of an
> exception (after optional handling of the exception).
>
> The catch-blocks for this have to:
>
> - leave the current scope (of the object initialization) after executing
> - forbid referencing the not-initialized object (could be checked by the
> compiler easily)
>
> My suggestion would look like this:
>
> try SomeObject object
> catch (const std::exception &e)
> {
> // this acts like a finally-block to clean-up stuff that was created
> before the initialization of object
> // and to handle the exception itself.
> // the scope of "try SomeObject object" is left at the end of this
> block.
> }
>
> Because the current scope could be a function that has a return type and
> the return value would be undefined in this case, I have two suggestions
> for this issue:
>
> 1. re-throw the exception out of the current scope or terminate the
> application if the scope is the main entry point
> 2. the compiler forces a valid return-statement at the end of each of
> these catch-blocks
>
> This won't break RAII at all, as the scope is left if the object is not
> created.
>
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_37_1476523460.1411683043531
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><span style=3D"font-size: 13.3333330154419px;">Sorry, perh=
aps a stupid question, but if the object needs to be valid the whole time w=
hile the application is running (this is what I understand from the comment=
in your first code snippet), why can't you simply put the loop inside the =
try/catch block as well?<br></span><br><div class=3D"prettyprint" style=3D"=
border: 1px solid rgb(187, 187, 187); word-wrap: break-word; background-col=
or: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprett=
yprint"><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136)=
;"><span style=3D"color: #008;" class=3D"styled-by-prettify">int</span></sp=
an><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> main</span></span><s=
pan style=3D"font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><span=
style=3D"color: #660;" class=3D"styled-by-prettify">(</span></span><span s=
tyle=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136);"><span style=
=3D"color: #008;" class=3D"styled-by-prettify">int</span></span><span style=
=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> argc</span></span><span style=3D"f=
ont-size: 13.3333330154419px; color: rgb(102, 102, 0);"><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">,</span></span><span style=3D"font-s=
ize: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span></span><span style=3D"font-size: 13.3=
333330154419px; color: rgb(0, 0, 136);"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">char</span></span><span style=3D"font-size: 13.3333=
330154419px; color: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span></span><span style=3D"font-size: 13.3333330154419=
px; color: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-=
by-prettify">**</span></span><span style=3D"font-size: 13.3333330154419px; =
color: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">argv</span></span><span style=3D"font-size: 13.3333330154419px; color:=
rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">)</span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(=
0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></=
span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(102, 1=
02, 0);"><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></span=
><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">try</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br></span></span><span sty=
le=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"> </spa=
n></span><span style=3D"font-size: 13.3333330154419px; color: rgb(102, 0, 1=
02);"><span style=3D"color: #606;" class=3D"styled-by-prettify">SomeObject<=
/span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0,=
0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span></s=
pan><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136);"><=
span style=3D"color: #008;" class=3D"styled-by-prettify">object</span></spa=
n><span style=3D"font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><=
span style=3D"color: #660;" class=3D"styled-by-prettify">;</span></span><sp=
an style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br><br>  =
; </span></span><span style=3D"font-size: 13.3333330154419px; color:=
rgb(0, 0, 136);"><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>while</span></span><span style=3D"font-size: 13.3333330154419px; color: rg=
b(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan></span><span style=3D"font-size: 13.3333330154419px; color: rgb(102, 10=
2, 0);"><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><=
/span><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><=
span style=3D"color: #000;" class=3D"styled-by-prettify">running</span></sp=
an><span style=3D"font-size: 13.3333330154419px; color: rgb(102, 102, 0);">=
<span style=3D"color: #660;" class=3D"styled-by-prettify">)</span></span><s=
pan style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #800;" class=3D"styled-by-prettify">// this is running for the whole ap=
plication lifetime (object must be valid the whole time)</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; </span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(=
102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-by-prettify">{<=
/span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0,=
0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span>=
</span><code style=3D"font-size: 13.3333330154419px;"><span style=3D"color:=
rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify">&=
nbsp; </span></span><span style=3D"color: rgb(102, 102, 0);"></span>=
</code><code style=3D"font-size: 13.3333330154419px;"><span style=3D"color:=
rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify">&=
nbsp; </span></span><span style=3D"color: rgb(102, 102=
, 0);"><span style=3D"color: #800;" class=3D"styled-by-prettify">// do some=
thing with object</span></span></code><span style=3D"color: #000;" class=3D=
"styled-by-prettify"><br></span><span style=3D"font-size: 13.3333330154419p=
x; color: rgb(0, 0, 0);"><code><span style=3D"color: rgb(102, 102, 0);"></s=
pan></code></span><span style=3D"font-size: 13.3333330154419px; color: rgb(=
102, 102, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify">&n=
bsp; </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">}</span></span><span style=3D"font-size: 13.3333330154419px=
; color: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br></span></span><span style=3D"font-size: 13.3333330154419px; colo=
r: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">catch</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify">std</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">exception </span><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">&)</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br> </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br> </span><span=
style=3D"color: #800;" class=3D"styled-by-prettify">// Whatever...</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></span><=
span style=3D"font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">}</span></span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></div></code=
></div><br>Cheers,<br><br>Andy<br style=3D"font-size: 13.3333330154419px;">=
<br style=3D"font-size: 13.3333330154419px;"><br><br><br>On Thursday, Septe=
mber 25, 2014 9:27:59 PM UTC+2, abym...@googlemail.com wrote:<blockquote cl=
ass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px =
#ccc solid;padding-left: 1ex;"><div dir=3D"ltr">The following pattern is of=
ten used in applications:<br><br><div style=3D"background-color:rgb(250,250=
,250);border-color:rgb(187,187,187);border-style:solid;border-width:1px;wor=
d-wrap:break-word"><code><div><span style=3D"color:#008">int</span><span st=
yle=3D"color:#000"> main</span><span style=3D"color:#660">(</span><span sty=
le=3D"color:#008">int</span><span style=3D"color:#000"> argc</span><span st=
yle=3D"color:#660">,</span><span style=3D"color:#000"> </span><span style=
=3D"color:#008">char</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">**</span><span style=3D"color:#000">argv</span><span style=
=3D"color:#660">)</span><span style=3D"color:#000"><br></span><span style=
=3D"color:#660">{</span><span style=3D"color:#000"><br> </span=
><span style=3D"color:#606">SomeObject</span><span style=3D"color:#000"> </=
span><span style=3D"color:#008">object</span><span style=3D"color:#660">;</=
span><span style=3D"color:#000"><br><br> </span><span style=3D=
"color:#008">while</span><span style=3D"color:#000"> </span><span style=3D"=
color:#660">(</span><span style=3D"color:#000">running</span><span style=3D=
"color:#660">)</span><span style=3D"color:#000"> // this is running for the=
whole application lifetime (object must be valid the whole time)<br> =
</span><span style=3D"color:#660">{</span><span style=3D"color:#000=
"><br></span><code><span style=3D"color:#000"> </span><span st=
yle=3D"color:#660"></span></code><code><span style=3D"color:#000"> &n=
bsp; </span><span style=3D"color:#660">// do something with object</span></=
code><br><span style=3D"color:#000"><code><span style=3D"color:#000"></span=
><span style=3D"color:#660"></span></code> </span><span style=
=3D"color:#660">}</span><span style=3D"color:#000"><br></span><span style=
=3D"color:#660">}</span><span style=3D"color:#000"><br></span></div></code>=
</div><br>Now I want to check if something goes wrong inside the initializa=
tion of object. I would now embed the initialization of object into a try-b=
lock and handle exceptions inside of a catch-block. But if I do so, the lif=
etime of object is bound to the try-block which is not my intention. I coul=
d now use a pointer and allocate/delete it at the right places but the only=
reason I would do this here is because the try-catch-syntax forces me to.<=
br><br>Of course a normal try-catch without scope change won't work either =
as I can ignore the exception in the catch-block and the object would not b=
e valid in case of an exception.<br><br>I thought of a try-expression for o=
bject initializations only which keeps the current scope but will always le=
ave the current scope in case of an exception (after optional handling of t=
he exception).<br><br>The catch-blocks for this have to:<br><br>- leave the=
current scope (of the object initialization) after executing<br>- forbid r=
eferencing the not-initialized object (could be checked by the compiler eas=
ily)<br><br>My suggestion would look like this:<br><br><div style=3D"backgr=
ound-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:soli=
d;border-width:1px;word-wrap:break-word"><code><div><span style=3D"color:#6=
06">try SomeObject object<br>catch (const std::exception &e)<br></span>=
<span style=3D"color:#660"></span>{<br> // this acts like=
a finally-block to clean-up stuff that was created before the initializati=
on of object<br> // and to handle the exception itself.<b=
r> // the scope of "<code><span style=3D"color:#606">try =
SomeObject object" is left at the end of this</span></code> block.<br>}</di=
v></code></div><br>Because the current scope could be a function that has a=
return type and the return value would be undefined in this case, I have t=
wo suggestions for this issue:<br><br>1. re-throw the exception out of the =
current scope or terminate the application if the scope is the main entry p=
oint<br>2. the compiler forces a valid return-statement at the end of each =
of these catch-blocks<br><br>This won't break RAII at all, as the scope is =
left if the object is not created.<br></div></blockquote></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_37_1476523460.1411683043531--
.
Author: abymaroth@googlemail.com
Date: Fri, 26 Sep 2014 04:34:12 -0700 (PDT)
Raw View
------=_Part_576_766968481.1411731252195
Content-Type: text/plain; charset=UTF-8
@Magnus: Ok this is the new initializer try syntax I guess. But as you
mentioned I can't react properly at the scope of object initialization
without another try-block there. So it would help that much.
@Andy: Of course this is possible but I consider large try-blocks as evil.
I would use try there to check if my object is properly created and don't
want to handle all exceptions that might occur inside the whole main
application loop. Normally in the main application loop thousands of
objects will be created. I want to catch specific exceptions at specific
points. Also consider, that after the initialization of object there might
be other objects that are initialized before the loop. If I would like to
handle each object initialization separately, your suggestion won't work
well. I won't know which object initialization caused the exception.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_576_766968481.1411731252195
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">@Magnus: Ok this is the new initializer try syntax I guess=
.. But as you mentioned I can't react properly at the scope of object initia=
lization without another try-block there. So it would help that much.<br><b=
r>@Andy: Of course this is possible but I consider large try-blocks as evil=
.. I would use try there to check if my object is properly created and don't=
want to handle all exceptions that might occur inside the whole main appli=
cation loop. Normally in the main application loop thousands of objects wil=
l be created. I want to catch specific exceptions at specific points. Also =
consider, that after the initialization of object there might be other obje=
cts that are initialized before the loop. If I would like to handle each ob=
ject initialization separately, your suggestion won't work well. I won't kn=
ow which object initialization caused the exception.<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_576_766968481.1411731252195--
.
Author: abymaroth@googlemail.com
Date: Fri, 26 Sep 2014 04:35:24 -0700 (PDT)
Raw View
------=_Part_203_1056864982.1411731325007
Content-Type: text/plain; charset=UTF-8
@Magnus: Ok this is the new initializer try syntax I guess. But as you
mentioned I can't react properly at the scope of object initialization
without another try-block there. So it wouldn't help that much.
@Andy: Of course this is possible but I consider large try-blocks as evil.
I would use try there to check if my object is properly created and don't
want to handle all exceptions that might occur inside the whole main
application loop. Normally in the main application loop thousands of
objects will be created. I want to catch specific exceptions at specific
points. Also consider, that after the initialization of object there might
be other objects that are initialized before the loop. If I would like to
handle each object initialization separately, your suggestion won't work
well. I won't know which object initialization caused the exception.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_203_1056864982.1411731325007
Content-Type: text/html; charset=UTF-8
<div dir="ltr">@Magnus: Ok this is the new initializer try syntax I guess. But as you
mentioned I can't react properly at the scope of object initialization
without another try-block there. So it wouldn't help that much.<br><br>@Andy:
Of course this is possible but I consider large try-blocks as evil. I
would use try there to check if my object is properly created and don't
want to handle all exceptions that might occur inside the whole main
application loop. Normally in the main application loop thousands of
objects will be created. I want to catch specific exceptions at specific
points. Also consider, that after the initialization of object there
might be other objects that are initialized before the loop. If I would
like to handle each object initialization separately, your suggestion
won't work well. I won't know which object initialization caused the
exception.</div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />
------=_Part_203_1056864982.1411731325007--
.
Author: Andy Prowl <andy.prowl@gmail.com>
Date: Fri, 26 Sep 2014 04:59:00 -0700 (PDT)
Raw View
------=_Part_100_1568613394.1411732740951
Content-Type: text/plain; charset=UTF-8
It is not necessary to have a large try-catch block: it's enough to extract
the loop into a separate function, and the try-catch block becomes minimal:
void loop(SomeObject& object)
{
while (running) // this is running for the whole application lifetime
(object must be valid the whole time)
{
// do something with object
}
}
int main(int argc, char **argv)
{
try
{
SomeObject object;
loop(object);
}
catch (std::exception const&)
{
// Whatever...
}
}
This also makes the dependency of your loop on "object" explicit, which is
a good thing IMO. If you have many of these objects and passing them all to
"loop()" becomes impractical, you can create one or more structures to hold
them, then pass that (or those) structure(s).
If you want your loop's errors to be handled separately from the object
initialization errors, you can wrap your loop in another try-catch block,
which could be made minimal again by extraction. That would catch your loop
errors. The outer try/catch would catch initialization errors.
For what concerns knowing which of the objects have failed, you're right,
you won't be able to tell which one it is unless you use nested try-catch
blocks, but is this fundamental? If the objects have different types (or
different roles based on their state), their failures could be expressed by
throwing exceptions of different types - this way you would be able to tell
what went wrong, which is what is fundamental IMO. Knowing concretely which
object threw the exception is less important than knowing what problem
occurred.
Anyway these are just my thoughts, good luck with your proposal :)
Andy
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_100_1568613394.1411732740951
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">It is not necessary to have a large try-catch block: it's =
enough to extract the loop into a separate function, and the try-catch bloc=
k becomes minimal:<br><br><div class=3D"prettyprint" style=3D"border: 1px s=
olid rgb(187, 187, 187); word-wrap: break-word; background-color: rgb(250, =
250, 250);"><code class=3D"prettyprint"><div class=3D"subprettyprint"><div =
class=3D"GO0BFSYAJB" style=3D"font-size: 13.3333330154419px;"><span style=
=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136);"><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">void</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> loop</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">(</span><span style=3D"color: #606;" class=
=3D"styled-by-prettify">SomeObject</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-=
prettify">object</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br=
></span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></span>=
<span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span></s=
pan><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136);"><=
span style=3D"color: #008;" class=3D"styled-by-prettify">while</span></span=
><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span></span><span st=
yle=3D"font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span></span><span style=
=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"col=
or: #000;" class=3D"styled-by-prettify">running</span></span><span style=3D=
"font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">)</span></span><span style=3D"font=
-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> </span><span style=3D"color: rgb(136, 0, 0=
);"><span style=3D"color: #800;" class=3D"styled-by-prettify">// this is ru=
nning for the whole application lifetime (object must be valid the whole ti=
me)</span></span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br> </span></span><span style=3D"font-size: 13.3333330154419p=
x; color: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">{</span></span><span style=3D"font-size: 13.3333330154419px; co=
lor: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br></span></span><code style=3D"font-size: 13.3333330154419px;"><span s=
tyle=3D"color: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> </span></span><span style=3D"color=
: rgb(102, 102, 0);"><span style=3D"color: rgb(136, 0, 0);"><span style=3D"=
color: #800;" class=3D"styled-by-prettify">// do something with object</spa=
n></span></span></code><span style=3D"font-size: 13.3333330154419px; color:=
rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"><=
br></span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(1=
02, 102, 0);"><span style=3D"color: rgb(0, 0, 0);"><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span></span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span></span><span style=
=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136);"><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"><br><br></span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">int</span></span><span style=3D"font-size: 13.3333=
330154419px; color: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> main</span></span><span style=3D"font-size: 13.333333015=
4419px; color: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span></span><span style=3D"font-size: 13.3333330154419p=
x; color: rgb(0, 0, 136);"><span style=3D"color: #008;" class=3D"styled-by-=
prettify">int</span></span><span style=3D"font-size: 13.3333330154419px; co=
lor: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> argc</span></span><span style=3D"font-size: 13.3333330154419px; color: =
rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-by-prettify=
">,</span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(0=
, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
></span><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 136)=
;"><span style=3D"color: #008;" class=3D"styled-by-prettify">char</span></s=
pan><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span></span><span=
style=3D"font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">**</span></span><span sty=
le=3D"font-size: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify">argv</span></span><span style=3D"=
font-size: 13.3333330154419px; color: rgb(102, 102, 0);"><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">)</span></span><span style=3D"font-=
size: 13.3333330154419px; color: rgb(0, 0, 0);"><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span></span><span style=3D"font-size: =
13.3333330154419px; color: rgb(102, 102, 0);"><span style=3D"color: #660;" =
class=3D"styled-by-prettify">{</span><span style=3D"color: rgb(0, 0, 0);"><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span></span>=
</span></div><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0,=
0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"> &nbs=
p; </span><span style=3D"color: rgb(0, 0, 136);"><span style=3D"color: #008=
;" class=3D"styled-by-prettify">try</span></span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br> </span><span style=3D"col=
or: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">{</span></span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br></span></span><div class=3D"GO0BFSYAJB" style=3D"font-size: 13.3333=
330154419px;"><span style=3D"font-size: 13.3333330154419px; color: rgb(0, 0=
, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"> &nb=
sp; </span></span><span style=3D"font-size: 13.3333330154419p=
x; color: rgb(102, 0, 102);"><span style=3D"color: #606;" class=3D"styled-b=
y-prettify">SomeObject</span></span><span style=3D"font-size: 13.3333330154=
419px; color: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> </span></span><span style=3D"font-size: 13.3333330154419px; co=
lor: rgb(0, 0, 136);"><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">object</span></span><span style=3D"font-size: 13.3333330154419px; colo=
r: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">;</span></span><span style=3D"font-size: 13.3333330154419px; color: rg=
b(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
<br></span></span><span style=3D"font-size: 13.3333330154419px; color: rgb(=
0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
loop</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">object</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">);</span></span></div><span style=3D"font-size: 13.3333330154419px; colo=
r: rgb(0, 0, 0);"><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: rgb(102, 102, 0);"><span style=
=3D"color: #660;" class=3D"styled-by-prettify">}</span></span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><sp=
an style=3D"color: rgb(0, 0, 136);"><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">catch</span></span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> </span><span style=3D"color: rgb(102, 102, 0);"><span=
style=3D"color: #660;" class=3D"styled-by-prettify">(</span></span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=3D=
"color: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styled-by-=
prettify">::</span></span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify">exception </span><span style=3D"color: rgb(0, 0, 136);"><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">const</span></span><span st=
yle=3D"color: rgb(102, 102, 0);"><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">&)</span></span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br> </span><span style=3D"color: rgb(102, =
102, 0);"><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span=
></span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> =
; </span><span style=3D"color: rgb(136, 0, 0);"><span =
style=3D"color: #800;" class=3D"styled-by-prettify">// Whatever...</span></=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &=
nbsp; </span><span style=3D"color: rgb(102, 102, 0);"><span style=3D"color:=
#660;" class=3D"styled-by-prettify">}</span></span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br></span></span><span style=3D"font-si=
ze: 13.3333330154419px; color: rgb(102, 102, 0);"><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">}</span></span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"><br></span></div></code></div><br>This also =
makes the dependency of your loop on "object" explicit, which is a good thi=
ng IMO. If you have many of these objects and passing them all to "loop()" =
becomes impractical, you can create one or more structures to hold them, th=
en pass that (or those) structure(s).<br><br>If you want your loop's errors=
to be handled separately from the object initialization errors, you can wr=
ap your loop in another try-catch block, which could be made minimal again =
by extraction. That would catch your loop errors. The outer try/catch would=
catch initialization errors.<br><br>For what concerns knowing which of the=
objects have failed, you're right, you won't be able to tell which one it =
is unless you use nested try-catch blocks, but is this fundamental? If the =
objects have different types (or different roles based on their state), the=
ir failures could be expressed by throwing exceptions of different types - =
this way you would be able to tell what went wrong, which is what is fundam=
ental IMO. Knowing concretely which object threw the exception is less impo=
rtant than knowing what problem occurred.<br><br>Anyway these are just my t=
houghts, good luck with your proposal :)<br><br>Andy</div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_100_1568613394.1411732740951--
.
Author: abymaroth@googlemail.com
Date: Fri, 26 Sep 2014 06:09:02 -0700 (PDT)
Raw View
------=_Part_58_637317185.1411736942604
Content-Type: text/plain; charset=UTF-8
A large try-block is not about code length but about the amount of
operations that happen inside them. You put the whole application into a
single try-block. That's the problem. Of course I can use a try inside the
outer try to explicitely catch exceptions from the loop. But consider that
this pattern also can be used inside another function and there I don't
want to catch loop-exceptions.
Nested try-blocks won't work at all for object initializations because they
will also bound the object lifetime to the nested scopes.
Different exceptions may be a possibility but why I have to design
exceptions for all objects? That makes no sense and the exception type
should not be related to the object but to the kind of error.
But thanks for you response.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_58_637317185.1411736942604
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><DIV>A large try-block is not about code length but a=
bout the amount of operations that happen inside them. You put the whole ap=
plication into a single try-block. That's the problem. Of course I can use =
a try inside the outer try to explicitely catch exceptions from the loop. B=
ut consider that this pattern also can be used inside another function and =
there I don't want to catch loop-exceptions.</DIV>
<DIV> </DIV>
<DIV>Nested try-blocks won't work at all for object initializations because=
they will also bound the object lifetime to the nested scopes.</DIV>
<DIV> </DIV>
<DIV>Different exceptions may be a possibility but why I have to design exc=
eptions for all objects? That makes no sense and the exception type should =
not be related to the object but to the kind of error.</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>But thanks for you response.</DIV></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_58_637317185.1411736942604--
.
Author: abymaroth@googlemail.com
Date: Fri, 26 Sep 2014 06:16:05 -0700 (PDT)
Raw View
------=_Part_29_1611625346.1411737365889
Content-Type: text/plain; charset=UTF-8
You also can initialize multiple objects of the same type. How you would
difference exception types at throwing then?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_29_1611625346.1411737365889
Content-Type: text/html; charset=UTF-8
<div dir="ltr">You also can initialize multiple objects of the same type. How you would difference exception types at throwing then?</div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />
------=_Part_29_1611625346.1411737365889--
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Fri, 26 Sep 2014 16:22:20 +0300
Raw View
On 26 September 2014 16:16, <abymaroth@googlemail.com> wrote:
> You also can initialize multiple objects of the same type. How you would
> difference exception types at throwing then?
Use std::experimental::optional and enclose the code that makes the optional
object contain a value in a try block.
That is, there is a library solution that is likely good enough that
we don't need
a language change.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Andy Prowl <andy.prowl@gmail.com>
Date: Fri, 26 Sep 2014 06:23:06 -0700 (PDT)
Raw View
------=_Part_616_1674805959.1411737786685
Content-Type: text/plain; charset=UTF-8
I was just trying to suggest a general guideline. My idea was: if you have
to initialize objects of different types, then those objects probably do
different things. If that's the case, it's likely that they will fail in
different ways, due to different situations, while doing different
operations, and that may be well expressed by using different exceptions.
Clearly, the above reasoning does not hold if you initialize objects of the
same type. But in general, I'd say what interests me when something goes
wrong is not who threw the exception, but what exactly happened. If a
configuration file could not be opened, it is not vital for me to know that
it was object "opener1" who threw the "access_denied_exception" rather than
"opener2": what matters to me is the error itself - including the name of
the file.
Sorry for misunderstanding what you meant by "big try-catch block".
Honestly I do not see that as a problem, but opinions may differ of course.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_616_1674805959.1411737786685
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I was just trying to suggest a general guideline. My idea =
was: if you have to initialize objects of different types, then those objec=
ts probably do different things. If that's the case, it's likely that they =
will fail in different ways, due to different situations, while doing diffe=
rent operations, and that may be well expressed by using different exceptio=
ns.<br><br>Clearly, the above reasoning does not hold if you initialize obj=
ects of the same type. But in general, I'd say what interests me when =
something goes wrong is not who threw the exception, but what exactly happe=
ned. If a configuration file could not be opened, it is not vital for me to=
know that it was object "opener1" who threw the "access_denied_exception" =
rather than "opener2": what matters to me is the error itself - including t=
he name of the file.<br><br>Sorry for misunderstanding what you meant by "b=
ig try-catch block". Honestly I do not see that as a problem, but opinions =
may differ of course.</div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_616_1674805959.1411737786685--
.
Author: David Krauss <potswa@gmail.com>
Date: Fri, 26 Sep 2014 21:22:30 +0800
Raw View
--Apple-Mail=_9F4A442F-2147-4145-BA65-447E5B915519
Content-Type: text/plain; charset=ISO-8859-1
On 2014-09-26, at 3:27 AM, abymaroth@googlemail.com wrote:
> try SomeObject object
> catch (const std::exception &e)
> {}
I think the closest we currently have is:
auto && object = [&]() -> SomeObject {
try {
return {};
} catch ( std::exception & e )
{}
}();
The rvalue reference avoids copying the return value object, but for movable types, copy elision will do the job too.
This is inline-block style, but you could also create a proper factory function encapsulating the construction and/or the exception handling.
This is semantically close enough to what you're going for... anything more is sugar.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
--Apple-Mail=_9F4A442F-2147-4145-BA65-447E5B915519
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;09–26, at 3:27 AM, <a href=3D"mailto:abymaroth@googlemail.com">=
abymaroth@googlemail.com</a> wrote:</div><br><blockquote type=3D"cite"><div=
dir=3D"ltr"><div class=3D"prettyprint" style=3D"background-color: rgb(250,=
250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word; po=
sition: static; z-index: auto;"><code class=3D"prettyprint"><div class=3D"s=
ubprettyprint"><span style=3D"color: #606;" class=3D"styled-by-prettify">tr=
y SomeObject object<br>catch (const std::exception &e)<br></span><span =
style=3D"color: #660;" class=3D"styled-by-prettify"></span>{}<br></div></co=
de></div></div></blockquote><div><br></div><div>I think the closest we curr=
ently have is:</div><div><br></div><div><font face=3D"Courier">auto &&a=
mp; object =3D [&]() -> SomeObject {</font></div><div><font face=3D"=
Courier"> try {</font></div><div><font face=3D"Courier"> =
return {};</font></div><div><font face=3D"Courier">&n=
bsp; } catch ( std::exception & e )</font></div><div><font face=
=3D"Courier"> {}</font></div><div><font face=3D"Courier">}();<=
/font></div><div><br></div><div>The rvalue reference avoids copying the ret=
urn value object, but for movable types, copy elision will do the job too.<=
/div><div><br></div><div>This is inline-block style, but you could also cre=
ate a proper factory function encapsulating the construction and/or the exc=
eption handling.</div><div><br></div><div>This is semantically close enough=
to what you’re going for… anything more is sugar.</div><div><=
br></div></div></body></html>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--Apple-Mail=_9F4A442F-2147-4145-BA65-447E5B915519--
.
Author: abymaroth@googlemail.com
Date: Fri, 26 Sep 2014 07:49:59 -0700 (PDT)
Raw View
------=_Part_436_818761325.1411742999586
Content-Type: text/plain; charset=UTF-8
@Ville: Interesting. Can you please give some short example how this may
look like?
@David: Does this also work for constructors with arguments? Even if this
may be a solution, it's much harder to understand the code. If I have to
use lambda expressions to handle exceptions of an object initialization,
this can't be a good thing.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_436_818761325.1411742999586
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><DIV>@Ville: Interesting. Can you please give some short e=
xample how this may look like?</DIV>
<DIV> </DIV>
<DIV>@David: Does this also work for constructors with arguments? Even if t=
his may be a solution, it's much harder to understand the code. If I have t=
o use lambda expressions to handle exceptions of an object initialization, =
this can't be a good thing.</DIV></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_436_818761325.1411742999586--
.
Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Fri, 26 Sep 2014 17:55:23 +0300
Raw View
On 26 September 2014 17:49, <abymaroth@googlemail.com> wrote:
> @Ville: Interesting. Can you please give some short example how this may
> look like?
optional<Whatever> my_obj;
try {
my_obj.emplace(ctor_args_for_Whatever);
} catch (WhateverYouWant) {
/* note that my_obj is still in scope here, but you can detect
that it doesn't hold a value */
}
/* note that my_obj is still in scope here, but you can detect that it
doesn't hold a value */
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: Magnus Fromreide <magfr@lysator.liu.se>
Date: Sat, 27 Sep 2014 03:04:46 +0200
Raw View
On Fri, Sep 26, 2014 at 09:22:30PM +0800, David Krauss wrote:
>
> On 2014-09-26, at 3:27 AM, abymaroth@googlemail.com wrote:
>
> > try SomeObject object
> > catch (const std::exception &e)
> > {}
>
> I think the closest we currently have is:
>
> auto && object = [&]() -> SomeObject {
> try {
> return {};
> } catch ( std::exception & e )
> {}
> }();
>
> The rvalue reference avoids copying the return value object, but for movable types, copy elision will do the job too.
>
> This is inline-block style, but you could also create a proper factory function encapsulating the construction and/or the exception handling.
>
> This is semantically close enough to what you're going for... anything more is sugar.
But this still suffer from the problem that the try block can't return from the
enclosing function, doesn't it?
/MF
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.
Author: David Krauss <potswa@gmail.com>
Date: Sat, 27 Sep 2014 10:20:25 +0800
Raw View
On 2014-09-27, at 9:04 AM, Magnus Fromreide <magfr@lysator.liu.se> wrote:
> But this still suffer from the problem that the try block can't return from the
> enclosing function, doesn't it?
The catch block needs to rethrow, yes. I should have illustrated that. If you want conditional construction and destruction, use Ville's suggestion.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
.