|
From: Michael C. <jd...@ho...> - 2001-09-05 07:34:41
|
Hi,
I have just tried this on the HPUX 10.20 aCC compiler and it does not
compile. The 'try' breaks it.
Enclosing the try/catch block within braces does compile.
Out of interest which page of Stroustroups book is this on ?
Regards,
Michael
---
Here is the compiled code for the HPUX...
#include <iostream.h>
class Exception {};
class Vector
{
int size;
public:
Vector(int s):size(s) {}
};
class X
{
Vector v;
public:
X(int);
};
X::X(int s) :v(s)
{
try
{
cout <<"code run!\n";
throw Exception();
}
catch(Exception)
{
cout <<"Exception caught!\n";
}
}
int main()
{
X x(3);
return 0;
}
-----
>From: "Ioannis Vranos" <no...@ya...>
>To: "Dev-C++" <dev...@li...>
>Subject: [Dev-C++] C++ question
>Date: Wed, 5 Sep 2001 06:24:36 +0200
>
>If anyone knows, please answer.
>
>The question is about exception handling caught by a constructor. Here
>is the code:
>
>class X
>{
>Vector v;
>// ...
>
>public:
>X(int);
>// ...
>};
>
>X::X(int s) try :v(s) // initialize v by s
>{
>// ...
>}
>
>catch(Vector::Size)
>{
>// exceptions thrown for v are caught here
>
>//...
>}
>
>------------------------------------------------------------------------
>--------------------------------------------
>
>This is from the bible. The question here is, the constructor's body
>ends at the end of catch body? if not, catch() belongs to where.
>
>This style compiles only for constructors defined outside of their
>class. For the non-believers code that compiles:
>
>#include <iostream>
>
>class Exception {};
>
>class Vector
>{
>int size;
>public:
>Vector(int s):size(s) {}
>};
>
>class X
>{
>Vector v;
>public:
>X(int);
>
>};
>
>X::X(int s) try :v(s)
>{
>std::cout<<"code run!\n";
>
>throw Exception();
>
>}
>
> catch(Exception)
> {
> std::cout<<"Exception caught!\n";
>
>
> }
>
>
>int main()
>{
>X a(7);
>}
>
>------------------------------------------------------------------------
>--------------------------------------------
>
>
>
>Regards,
>Ioannis
>
>* Ioannis Vranos
>* Programming pages: http://www.noicys.f2s.com
><http://www.noicys.f2s.com/>
>* Alternative URL: http://run.to/noicys
>
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|