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