The (un-?)popularity of try, throw and catch

Started by
23 comments, last by SkyDruid 23 years, 7 months ago
As far as I understand it, Microsoft creates their own standards if there''s any standards (regarding C# or Java ). And definitely doesn''t care for existing standards (But any compiler needs time to evolve ). In special I mean using the scope operator in a C++ class declaration which isn''t in any C++ specification

To CodyVa:

You cant''t compare a variable to a value if it is not declared and as menationed earlier you can''t access a variable outside you''re scope but the global scope.

Your code:

if (x != null) { ++x;} //if x is declared increment
else {int x = 0;} //if not declare it


For the compiler, this is like: compare the global variable x to zero and increment its value if it is so.
else declare a new variable x. Because the new variable is declared in a block, it is only valid and accesible in the block itself.

I think you can make it with a static variable, but then you should be aware to decrement the variable again when returning from the function. Because you never would be able to call the function again.

Another approach would be to use a function like this:

void func (int maxiterations)
{
if ( maxiterations > 0 )
if (somethinggoeswrong)
func(maxiterations-1);
}


Hope this helps
Advertisement
Personally, I love exceptions, but you''ve really got to use them properly. First lesson is that they are NOT meant for flow-control, they''re meant for exceptional situations (thus the name). There''s a cost associated with entering a try block and percolating an exception up the stack is not as fast as normal code (which isn''t a big deal if they only happen ocasionally). Second lesson is that exception specifications are of questionable merit. The "why" is a tad involved but I would reccommend checking item 14 of Scott Meyers'' "More Effective C++": "Use exception specifications judiciously." Basically, they''re more trouble than they''re worth. Finally, never throw an exception out of a destructor. Bad Things (tm) happen (like your program crashing).

Why are exceptions so great? Instead of filling your code with error recovery routines you can centralize them. Also, rather than checking the return value of every function call, you can assume that it was successful if an exception was not thrown. This doesn''t work for every situation, but it''s nice not have to put every function call in a "if" block. Another handy way to manage things (either as an alternative to exceptions or in addition to them) is to put some error checking and recovery code in an inline function. It''s much safer than macros but expands to effectively the same thing. Something like the following:

inline void Try( int retval ) {
if( retval ) {
// something went wrong
}
}

Try( MyFunc( param1, param2 ) );

A pretty handy way to code error stuff once but use it all over. I do this in situations where I don''t want to throw exceptions or at times when I need to examine a retval a bit more closely before determining whether to throw or not (like network code where some error values actually represent a success condition).
Shouldn''t Microsoft have to follow the ANSI\ISO C++ standards to be able to implement C++ compiler?



-=[ Lucas ]=-
-=[ Lucas ]=-
microsoft doesn''t look at standarts. another example showing this is STRUCT

normaly, the struct came from C, and doesn''t support methods, private and things like it, but in VC you could take every class, and change the word class into struct.. nothing changes, so I (started and always used VC) really don''t know, how I could programm reall C-Code in VC. Even whitout using all the C++ stuff, you don''t know if, what you use is really normal C stuff (I wont start talking about MFC)

to the exeption handling:
I never understood try catch and this, so I never used it. and my one and only thing, I use, is TRUE and FALSE.. every function returns a value, that could be TRUE and FALSE, and with only this little errorhelp, I never had problems with anything (onetime my programm got an error with the mouse and I had to re-install my complete windows, loosing all the data, but.. no, no problems)
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

davepermen:
The ANSI C++ standard says that you can put functions in a struct (then they''re called methods). The only difference between a class and a C++ struct is that in a class everything is private by default, and in a struct it''s public.

And yes, Micro$oft is still allowed to make a C++ compiler without following the ANSI standard. They just can''t call it an "ANSI C++ compiler".

But to get back to the point: I do use exceptions often. I think they''re cool and easier to use than return codes. I use them more in Java than in C++, but I think that''s because in Java, you don''t have to keep track of all your memory allocations (with the garbagecollector and all), so it''s easier than in C++.

Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page

This topic is closed to new replies.

Advertisement