Topic: deprecate catch exception by value, at least in some instances?


Author: gmisocpp@gmail.com
Date: Sun, 8 Feb 2015 14:42:56 -0800 (PST)
Raw View
------=_Part_1781_414549599.1423435376514
Content-Type: multipart/alternative;
 boundary="----=_Part_1782_898159525.1423435376514"

------=_Part_1782_898159525.1423435376514
Content-Type: text/plain; charset=UTF-8

Should catching by value deprecated?
At least the catching by value of types that don't suit being caught by
value.

#include <cstddef>
#include <cstdio>
#include <stdexcept>
int main( int argc, char* argv[])
{
    char* p = nullptr;
    int status;
    try
    {
        std::size_t n = 1000000000000;
        p  = new char[n];
        status = 1;
        delete[] p;
    }
    catch(std::exception e) // slice
    {
        status = -1;
        std::printf("Caught\n");
    }
    return status;
}

Here e is std::exception caught by value. But it's a bad_alloc which
derives from exception that is thrown.
Shouldn't the compiler at least warn about this?

A more graphic example  note my_base destructs twice but constructs once:

#include <cstdio>

class my_base
{
public:
    my_base()
    {
        std::printf("my_base\n");
    }
    ~my_base()
    {
        std::printf("~my_base\n");
    }
};
class my_derived : public my_base
{
public:
    my_derived()
    {
        std::printf("my_derived\n");
    }
    ~my_derived()
    {
        std::printf("~my_derived\n");
    }
};
void f()
{
    throw my_derived();
}
int main( int argc, char* argv[])
{
    char* p = nullptr;
    int status;
    try
    {
        f();
    }
    catch(my_base e)
    {
        status = -1;
        std::printf("Caught\n");
    }
    return status;
}

Again no warning. Would anyone ever want this in this circumstance?

Thanks

--

---
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_1782_898159525.1423435376514
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Should catching by value deprecated?</div><div>At lea=
st&nbsp;the catching by value of types that don't suit being caught&nbsp;by=
 value.</div><div><br></div><div>#include &lt;cstddef&gt;<br>#include &lt;c=
stdio&gt;<br>#include &lt;stdexcept&gt;<br>int main( int argc, char* argv[]=
)<br>{<br>&nbsp;&nbsp;&nbsp; char* p =3D nullptr;<br>&nbsp;&nbsp;&nbsp; int=
 status;<br>&nbsp;&nbsp;&nbsp; try<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::size_t n =3D 1000000000000;<br>&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p&nbsp; =3D new char[n];<br>&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status =3D 1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delete[] p;<br>&nbsp;&n=
bsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; catch(std::exception e) // slice<br>&nbs=
p;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status =3D -=
1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::printf("Caught\n");<b=
r>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; return status;<br>}<br>&nbsp;&=
nbsp;&nbsp; </div><div>Here&nbsp;e is std::exception&nbsp;caught by value. =
But it's a&nbsp;bad_alloc which derives from exception that&nbsp;is thrown.=
</div><div>Shouldn't the&nbsp;compiler at least&nbsp;warn about this?</div>=
<div><br></div><div>A more graphic example&nbsp; note my_base destructs twi=
ce but constructs once:</div><div><br></div><div>#include &lt;cstdio&gt;</d=
iv><div><br></div><div>class my_base<br>{<br>public:<br>&nbsp;&nbsp;&nbsp; =
my_base()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp; std::printf("my_base\n");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbs=
p;&nbsp; }<br>&nbsp;&nbsp;&nbsp; ~my_base()<br>&nbsp;&nbsp;&nbsp; {<br>&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::printf("~my_base\n");<br>&nbsp;=
&nbsp;&nbsp; }<br>};</div><div>class my_derived : public my_base<br>{<br>pu=
blic:<br>&nbsp;&nbsp;&nbsp; my_derived()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::printf("my_derived\n");&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; ~my_deri=
ved()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 std::printf("~my_derived\n");<br>&nbsp;&nbsp;&nbsp; }<br>};</div><div>void=
 f()<br>{<br>&nbsp;&nbsp;&nbsp; throw my_derived();<br>}</div><div>int main=
( int argc, char* argv[])<br>{<br>&nbsp;&nbsp;&nbsp; char* p =3D nullptr;<b=
r>&nbsp;&nbsp;&nbsp; int status;<br>&nbsp;&nbsp;&nbsp; try<br>&nbsp;&nbsp;&=
nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; f();<br>&nbsp;&nbsp;&=
nbsp; }<br>&nbsp;&nbsp;&nbsp; catch(my_base e)<br>&nbsp;&nbsp;&nbsp; {<br>&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; status =3D -1;<br>&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; std::printf("Caught\n");<br>&nbsp;&nbsp;&nbsp; =
}<br>&nbsp;&nbsp;&nbsp; return status;<br>}</div><div><br></div><div>Again =
no warning. Would anyone ever want this in this circumstance?</div><div><br=
></div><div>Thanks<br></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&quot; 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_1782_898159525.1423435376514--
------=_Part_1781_414549599.1423435376514--

.