Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/doc/Exception


Ignore:
Timestamp:
Sep 4, 2008, 10:26:02 AM (16 years ago)
Author:
rgrieder
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/Exception

    v1 v1  
     1= Exceptions, Assertions and COUT =
     2Whenever something unwanted happens, the programmer needs to react in a certain way.[[br]]
     3We support three different ways to handle such situations:
     4 * Display a message in the console, shell and log
     5 * Throw an exception that can be caught
     6 * Abort the program with a message
     7[[br]]
     8
     9== COUT(#), displays messages ==
     10Whenever you want to show the user or the programmer a message, use COUT(#) where # is a number denoting the output level:
     11|| # || Meaning ||
     12|| || ||
     13|| 0 || Gets always displayed and is mean for messages like "loading level 'foo.bar'" ||
     14|| 1 || Errors. Something seriously bad has happened. Mostly in combination with assert() or an exception. ||
     15|| 2 || Warnings. The issue is not too bad, but everyone should see that something bad has happened. ||
     16|| 3 || Slight debug information. Not intended to be displayed to the end-user, but surely every beta tester. ||
     17|| 4 || Debug information. Can be quite a lot of text. ||
     18|| 5 || Verbose debug information. That would be overkill in the shell or console. View the log! ||
     19|| 6 || Extreme debug information. You better use grep or another regex tool to read a log file for level 6. ||
     20[[br]]
     21
     22You can configure the displayed levels for all output devices: Shell, console and log. Default value is around 3. There is also a possibility to define a hard debug level, so that the COUT(#) calls don't even get compiled above a that level. [[br]][[br]]
     23'''Note: A simple message with level 1 doesn't trigger an exception or anything yet!'''
     24
     25== Exceptions ==
     26This kind of error handling method is used when the programmer has to handle situations that could go wrong, but shouldn't. This does not count obvious C++ programming mistakes! (see Assertions below) [[br]]
     27When an exception gets thrown its message, containing file, line number, function name, exception type and programmer message, is displayed via COUT(4). That means you usually don't see them anywhere because it is better left to the programmer to display the error at the right position. But you can still open the log and search for it unless the log debug level is below 4. [[br]][[br]]
     28
     29To throw an exception you best use the 'ThrowException(type, message)' macro that automatically adds line number, function and file name. 'Type' is a user defined derived class of orxonox::Exception so that different exceptions can be caught with different 'catch' clauses. [[br]]
     30Currently available types are: [[br]]
     31|| Name || Description ||
     32|| || ||
     33|| General || Anything. Avoid using it and rather define a new one. ||
     34|| FileNotFound || Obviously a file was not found ||
     35|| Argument || A function argument in a parser was wrong. ||
     36|| PluginsNotFound || No Ogre plugins were found but are required ||
     37|| InitialisationFailed || When starting the game engine, something went terribly wrong. ||
     38|| NotImplemented || The feature requested has not yet been implemented ||
     39|| GameState || Something went wrong with a GameState class ||
     40[[br]]
     41
     42== Assertions ==
     43Last but not least: Assertions. This is very handy programmer's tool. It helps a lot tracking bugs and sets up a newly created class much quicker. [[br]]
     44When should you use it? Basically, an assertion is used when the programmers assumes that a certain condition is true and that otherwise the program would fail somewhere. [[br]][[br]]
     45An example: In a function, you receive a pointer and do something like ptr->aMemberFunction() while assuming that ptr != 0 because otherwise, you'll probably get a segmentation fault.
     46
     47{{{
     48void fooBar(myClass* ptr)
     49{
     50    ...
     51    ptr->aMemberFunction();
     52}
     53}}}
     54
     55You can now insert a call to the 'asser()' macro so that whenever ptr == 0 the program aborts. Now this doesn't sound very help, but it is: First of all, the abort message tells you exactly where the error has happened (file, line, function). Secondly, the program would have aborted anyway because of the null pointer. [[br]][[br]]
     56The more asserts you insert, the easier bug tracking will be. You might even spot them before they could ever be triggered. [[br]][[br]]
     57'''Important: Asserts are only useful when the mistake is in the program. Throwing asserts for bad input doesn't help anyone, use exceptions then!''' [[br]]
     58
     59Usage: 'assert(condition you assume);' or when you want to tell more use 'OrxAssert(condition, message)'. The message gets then displayed via COUT(1) before the assert() macro is called. [[br]]
     60The example above would then read:
     61
     62{{{
     63void fooBar(myClass* ptr)
     64{
     65    ...
     66    OrxAssert(ptr != 0, "You screwed the wrong pointer!");
     67    ptr->aMemberFunction();
     68}
     69}}}