• Advertisement
Sign in to follow this  

Exceptions & namespaces

This topic is 4143 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all! Well I´m prototyping a game for a project using C++ that is going to be quite long for my standards (at least 4 months I presume), so I´ve been pondering on some questions regarding code organization and error handling: 1. Is using exceptions the best way to make my code handle errors properly? I´m used to writing conditionals galore to catch them, but maybe exceptions provide better performance? 2. If so, are there any pre-built exceptions on Visual C++ 6.0? Or do I have to write an exception class? I know when using .NET Framework you can do both, but is that something only for .NET or a standard C++ feature? 3. I´m using namespaces to separate groups of functions with different general objectives on my code. Is this a good thing in terms of organization, or should I divide them in classes as static functions (I suppose instantiating a class just to use a function, the class having no attributes at all, is a dumb thing to do)? What´s the underlying difference between them? And also, where could I find a good exception handling tutorial using C++? I´ve read some but they don´t seem to be very good. Thanks in advance, Carol

Share this post


Link to post
Share on other sites
Advertisement
I'd recommend reading this interview with Bjarne Stroustrup for some discussion on the use and misuse of classes.

It's easy to go overboard with classes. Not everything needs to be a class. Some things don't make sense as classes. Sometimes you don't even need to hide every single data member of a class. If a function doesn't need access to the internal representation of a class it doesn't need to be a member function and classes aren't meant to group functions together as libraries. He touches on some of those issues in this interview and on his site.

It's a good idea to encapsulate your data structues and functions in namespaces and provide interfaces to that namespace for maintainability issues.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You mentioned Visual Studios 6.0. Stop right there, drop it, delete it and break the CDs if you still have them. Once broken, burn them. VC6 is horrifically broken and doesn't even support the C++ standard as it is today or when it was first "standardized". Get the FREE express edition. Make your life a LOT easier.

Share this post


Link to post
Share on other sites
Huh... you say VC6 doesn´t follow standards, but before using it I was stuck with Dev-C++ and I must say VC6 is a lot better, and it seems to be the same C++ as Dev´s. I got it free from college so why not use it? Besides, the Express edition uses C++ with .NET only, right? Although I guess .NET really makes life easier, I´d rather program my GUI using GDI+ just so I can learn how (I´m just making a supposition here) .NET forms and stuff like that work behind the scene.
I´d appreciate opinions on that, though =)

Carol

Share this post


Link to post
Share on other sites
no, the express edition is not .NET only. You just have to download the platform SDK seperately and install it too. do a search for either "visual C++ express edition native app" or "Visual C++ express edition platform sdk" because there are many beginners here that have asked that question. Also, Visual Studio 2005 in general trips people up because it changed the default string / unicode options from VC 6 and VS 2003 - so you may need to read a few topics on that too, but the gist is you will probably want to change your project's string options too.

VC 7.1 (2003) was perhaps the very best native C++ compiler because it had very good standards support without changing the options people we're used to. VS 8 (2005) can be just as good, you just have to be aware of some changes.

Share this post


Link to post
Share on other sites
VC6 isn't really as bad as everybody makes it out to be. It is however about 8 years old making it positively ancient in computer years and support for modern coding styles is rather lacking. There is really no reason whatsoever to use it, especially when the most recent VC/C++ is available for free.

1. "best" is subjective. There have been several flamewars on this board about whether or not exceptions are worth using at all, and if so, to what degree. Having said that, I like them. Take that for what you will.

2. There really isn't any reason not to derive your app-specific exceptions from std::exception or one of it's standard subclasses IMHO.

3. If the choice is between free functions in namespaces and static member functions in otherwise empty classes it doesn't really matter IMHO. I would rate the latter rather higher on the wtf? scale though.

Share this post


Link to post
Share on other sites
Just jumping by to support the destruction of VC6 :-)
The VC6 compiler was written before the c++ standard was released in 98, and so it doesn't support MANY important c++ features. Dev c++ is working with the gcc compiler, which is much better than VC6's compiler, but the enviroment isn't that good. VC2003/2005 is the way to go, as Xai said above.

As for your other question: exceptions are great (though very heavy on the code size, if that matters to you for some reason). I know no other convenient way to handle errors, so using exceptions is definitly the way to go.

Share this post


Link to post
Share on other sites
Quote:
Original post by Carolina
I got it free from college so why not use it?


Because it's like AIDS.

Remember kids, just say no to unprotected sex no longer officially supported products (Microsoft no longer supports DirectX for VS6), which have been superceeded by vastly superior solutions (such as the free Visual Studio 2005 Express) without doing your homework first (which you have not, if you've laboured under the false assumption that VS2005 is .NET only).

In seriousness, I don't consider VS6 a C++ compiler. It predates the standard, and as such deviates heavily from the C++ standard in terms of both templates and it's standard library.

Share this post


Link to post
Share on other sites
Wow, so NOW I know why VC6 was so easy to get! Well, then, guess it´s bye bye to this ancient & evil thing, and I´ll download the Express version XD

Share this post


Link to post
Share on other sites
You only got an 8 year old piece of software free from college? I got WinXP and Visual Studio 2003(I can get 2005 as well, I just had a class that wanted us to use 2003). I can get Visio as well as several other MS products for the cost of the CDs it takes to hold the installation disks. Not trying to gloat, but are you sure your college doesn't have access to the Microsoft Developer Network Academic Alliance(MSDN AA)? This program allows participating colleges to give out a lot of software for free to students in a computer discipline. About the only thing they Don't have is MS Office.

Just something to look into.

Share this post


Link to post
Share on other sites
I'm still using VC6 at home because it's all I have. I've never had issues programming C++, but maybe I haven't been using the features it's missing. Is there a list of the problems with VC6?

I had used the 2003 toolkit in the past while still using the VC6 GUI, but I couldn't develop MFC with that.

I'm going to try the 2005 Express edition out as well.

Share this post


Link to post
Share on other sites
Off the top of my head, proper variable scoping in for loops, and partial template specialisation are two big things missing from VC6 (although there are many others).

You would be more likely to run into trouble if you use certain libraries that make heavy use of templates.

Some features of the boost libraries, for example, are unsupported under VC6, but supported on VC .NET 2003 and 2005. Later versions of the boost::spirit parser library for example, will not compile at all under Visual Studio compilers older than Visual Studio 2003. Some boost libraries have nicer features available when you use standard conforming compilers, compared to compilers like Visual Studio 6 or 2002. (see boost::function for an example)

I've got Visual Studio 2002 professional, and I'm still using VC2005 Express at the moment, because the compiler is just so much better than Visual Studio 2002, that I don't mind using the express edition until I can find some spare cash to get the professional edition.

Share this post


Link to post
Share on other sites
XD
In fact, I can get all Microsoft stuff, as well as softwares like MatLab for free at college. We even have a Microsoft Lab (and I´ve been working there as an intern for almost a month¬¬)...but then, my laziness got the best out of me and I asked my college friend for the Visual C++ she had so I wouldn´t have to go there this weekend. Not the best of ideas it seems >.<"
But, hey, I like using emacs and g++, nothing bothers me any longer XD

Share this post


Link to post
Share on other sites
Quote:
Original post by Carolina
1. Is using exceptions the best way to make my code handle errors properly? I´m used to writing conditionals galore to catch them, but maybe exceptions provide better performance?

That depends on the error. Sometimes, yes, sometimes, no. If you search the forums, there are a number of threads discussing when they're appropriate.
Quote:
Original post by Carolina
2. If so, are there any pre-built exceptions on Visual C++ 6.0? Or do I have to write an exception class? I know when using .NET Framework you can do both, but is that something only for .NET or a standard C++ feature?

C++ lets you throw pretty much anything as an exception. throw 2; works just fine, although you shouldn't ever do that. The standard library does provide a number of exceptions to cover its own needs, but you're free to create your own if you want. A quick google yields this if you're interested in the standard ones.
Quote:
Original post by Carolina
3. I´m using namespaces to separate groups of functions with different general objectives on my code. Is this a good thing in terms of organization, or should I divide them in classes as static functions (I suppose instantiating a class just to use a function, the class having no attributes at all, is a dumb thing to do)? What´s the underlying difference between them?

A 'static' class [one with just static members] offers no advantage over a namespace, and in general you should prefer namespaces in those circumstances. Whether or not such namespaces are a good organization depends on what you're doing, but don't shy away from them just because you read somewhere that freestanding functions are bad.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Carolina
1. Is using exceptions the best way to make my code handle errors properly? I´m used to writing conditionals galore to catch them, but maybe exceptions provide better performance?


As far as I know, the performance is something like this:
Exceptions are basically free *when they're not thrown*
But they're a hell of a lot slower than an if-statement once they are thrown.

So from a pure performance point of view, you should only use them when you don't expect them to be thrown, but it might possibly happen once in a while.

And luckily, from a design point of view, I'd do much the same. Use exceptions for, well, exceptional errors. Things that aren't ever supposed to happen, but sometimes happen anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Quote:
Original post by Carolina
1. Is using exceptions the best way to make my code handle errors properly? I´m used to writing conditionals galore to catch them, but maybe exceptions provide better performance?


As far as I know, the performance is something like this:
Exceptions are basically free *when they're not thrown*
But they're a hell of a lot slower than an if-statement once they are thrown.


Which is still pretty darn fast. I recently posted some benchmarks of this.

Relevant release mode statistics using VS2005 Standard on my 2.4Ghz P4:

~126 thousand exceptions thrown, caught, and tallied/second.
~1.686 billion (american - 109) try/catch blocks entered and left/second.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement