Skip to main content
GameDev.net gamedev.net
🔒 Locked

Which error do I use?

Started by Alpha_ProgDes Oct 5, 2002 at 7:09 AM 1 replies 300+ views
Original Post
Alpha_ProgDes
Alpha_ProgDes
in what situation would use a assert over a try/catch? or are they both the same thing, just different ways of implementing error catching?
Beginner in Game Development?  Read here. And read here.  
thona
thona
Assert''s normally are thron out when you switch the compiler to RELEASE.

I use Asswerts for precondition/postcondition verification during development, while try/catch is for "typical runtime errors".

Means, if I have a function
void x (int *a, int *b)

where I want pointer to be turned in, then I would write
void x (int *a, int *b) {
ASSERT (a != 0)
ASSERT (b != 0)

to make sure I use the function correctly. In the release version, these asserts are thrown out by the compiler.

For example, lets have a function deleting a file.
void DeleteFile (char *filename);

Now, ASSERT would be:
- there is a filename.

TRY/CATCH would be:
- file does exist.

Means: in my program I would not and never call the function with a null pointer as filename. Obviously - I want this to be seen immediatly during debugging.

Whether the file exists or not, though, is a condition I also need to handle during runtime :-)


Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
RegardsThomas TomiczekTHONA Consulting Ltd.(Microsoft MVP C#/.NET)
Kylotan
Kylotan
Asserts are to catch errors that should never exist - mistakes made by the programmer. They guarantee the correctness of the program''s structure, and are a development tool. Exceptions are to catch errors generated by the environment, such as the user, the files, lack of memory, etc. They guarantee correct program operation under unusual conditions, and are part of the end product.

That''s an oversimplification, but should give you the right idea.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.