C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

please suggest

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
pushkal
Guest





PostPosted: Tue Sep 05, 2006 5:36 pm    Post subject: please suggest Reply with quote



Hi All,

I am new to this group, not to mention with a miniscule knowledge on
C++.
If you have time:

This Silly piece of code ( coded by me) aborts on execution.
offcourse this has no exception object to be thrown.

Please suggest is there a technique to stop such a bad code from
compiling. which might affect a big project too.

/////////////////////////////////
#include<iostream.h>
main()
{
try
{
cout<< " before throw " << endl ;
throw ;
cout<< " after throw" << endl ;
}
catch ( ... )
{
cout << "inside catch "<< endl ;
}
}
//////////////////////////////////////

Regards
Pushkal


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeffrey Schwab
Guest





PostPosted: Tue Sep 05, 2006 7:24 pm    Post subject: Re: please suggest Reply with quote



pushkal wrote:
Quote:
Hi All,

I am new to this group, not to mention with a miniscule knowledge on
C++.

Welcome aboard.

Quote:
#include<iostream.h
main()
{
try
{
cout<< " before throw " << endl ;
throw ;
cout<< " after throw" << endl ;
}
catch ( ... )
{
cout << "inside catch "<< endl ;
}
}

#include <iostream>

int main() try {
std::cout << "before throw\n";

// Outside catch blocks, throw
// needs an argument.
throw "an exceptional string?";

std::cout << "after throw\n";
} catch(...) {
std::cerr << "inside catch\n";
}

You can't throw unless you're throwing something. Inside a catch block,
you can use "throw;" to re-throw the current exception, but outside
catch blocks, "throw" needs an argument.

Btw, you probably want to use "iostream" instead of "iostream.h", and
"int main()" instead of "main()".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ulrich Eckhardt
Guest





PostPosted: Tue Sep 05, 2006 7:24 pm    Post subject: Re: please suggest Reply with quote



pushkal wrote:
Quote:
I am new to this group, not to mention with a miniscule knowledge on
C++.

You should read the FAQ and pick a topic that resembles your problem.

Quote:
This Silly piece of code ( coded by me) aborts on execution.
offcourse this has no exception object to be thrown.

Please suggest is there a technique to stop such a bad code from
compiling. which might affect a big project too.
[...]
#include<iostream.h

Many compilers will already stop compiling here, because that header is not
part of standard C++ and thus only sometimes included for backward
compatibility.

Quote:
main()

Many more will stop here, because an implicit int returntype has never
existed in C++.

Quote:
{
try
{
cout<< " before throw " << endl ;
throw ;
cout<< " after throw" << endl ;
}
catch ( ... )
{
cout << "inside catch "<< endl ;
}
}

Trying to rethrow an exception without being in a catch handler is not
necessarily possible at compile-time, consider this rethrow statement was
in a function that might be called in different contexts. It would take a
lot of effort to detect that.

Also, I think the semantics are well-defined for this case to invoke abort()
or a similarly non-graceful way of terminating or invoking the debugger,
there is nothing wrong about this.

Lastly, looking at the code (in particular the two points that should have
kept it from compiling) it seems to me that you should a) update your
compiler to anything even halfway modern and b) that you should update your
C++ knowledge. For the former, it depends on your platform, for the latter
you should pick a good book from the reviews at http://accu.org.

cheers

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
loufoque
Guest





PostPosted: Tue Sep 05, 2006 7:27 pm    Post subject: Re: please suggest Reply with quote

{ that's a good point, but this is the second reply that is not addressing
the question asked. could we please be more attentive? thanks! -mod }


pushkal wrote :

Quote:
#include<iostream.h

The standard C++ header is <iostream>, and all elements are in the std
namespace.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Hrayr BABAJANYAN
Guest





PostPosted: Tue Sep 05, 2006 7:52 pm    Post subject: Re: please suggest Reply with quote

Hi!
pushkal wrote:
Quote:
Hi All,

I am new to this group, not to mention with a miniscule knowledge on
C++.
If you have time:

This Silly piece of code ( coded by me) aborts on execution.
offcourse this has no exception object to be thrown.

Please suggest is there a technique to stop such a bad code from
compiling. which might affect a big project too.

/////////////////////////////////
#include<iostream.h
main()
{
try
{
cout<< " before throw " << endl ;
throw ;
cout<< " after throw" << endl ;
}
catch ( ... )
{
cout << "inside catch "<< endl ;
}
}
//////////////////////////////////////

Regards
Pushkal


IMHO: No! It will compile always, you can newer guarantee that someone
is not using in some unexpected place 'throw;' statement. The only
thing that can help (I think) is a good coverage of your code.

Sorry nothing else comes to my mind.

Cheers!
--
Hrayr


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Tue Sep 05, 2006 10:08 pm    Post subject: Re: please suggest Reply with quote

pushkal wrote:

Quote:
This Silly piece of code ( coded by me) aborts on execution.
offcourse this has no exception object to be thrown.

Please suggest is there a technique to stop such a bad code
from compiling.

Well, you're code doesn't compile with any compliant
compiler:-). But that is for reasons totally unrelated to the
question you are asking.

Quote:
which might affect a big project too.

Big projects test, and have code review. And only throw is very
limited, documented cases.

Quote:
/////////////////////////////////
#include<iostream.h

This is an out of date header, not fully supported by most of
the compilers I use. Try:

#include <iostream>
#include <ostream>

(Most of the compilers do have a header with the name
<iostream.h>, which may contain enough to compile the code
you've written. But except for VC++, the versions I've seen
have not been adequate to compile real older code which used the
older header. At any rate, if you're learning, you really
should limit yourself to the standard headers, at least until
you really have to deal with legacy code.)

Quote:
main()

And this shouldn't compile either. You simply cannot declare a
function without specifying its return type. In the case of
main, this is always int, do you must write:

int main()

Quote:
{
try
{
cout<< " before throw " << endl ;

Of course, with the new headers, you'll need to write:

std::cout << "..." << std::endl ;

Quote:
throw ;

And this is your question. In practice, there's really no way
for the compiler to detect the error in all cases, so the
standard doesn't require it.

The number of cases where you need a throw like this are
exceedingly rare, and almost always within an immediate catch
block. In practice, I cannot imagine such a mistake getting
through code review.

Quote:
cout<< " after throw" << endl ;
}
catch ( ... )
{
cout << "inside catch "<< endl ;
}
}
//////////////////////////////////////

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.