Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/Error_handling


Ignore:
Timestamp:
Sep 4, 2008, 2:29:47 PM (16 years ago)
Author:
rgrieder
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/Error_handling

    v1 v1  
     1= Error handling in Orxonox =
     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 [wiki:Debug level]. [[br]]
     11
     12'''Note: A simple message with level 1 doesn't trigger an exception or anything yet!''' [[br]]
     13
     14== Exceptions ==
     15This 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]]
     16When 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]]
     17
     18To 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]]
     19Currently available types are: [[br]]
     20|| Name || Description ||
     21|| || ||
     22|| General || Anything. Avoid using it and rather define a new one. ||
     23|| FileNotFound || Obviously a file was not found ||
     24|| Argument || A function argument in a parser was wrong. ||
     25|| PluginsNotFound || No Ogre plugins were found but are required ||
     26|| InitialisationFailed || When starting the game engine, something went terribly wrong. ||
     27|| NotImplemented || The feature requested has not yet been implemented ||
     28|| GameState || Something went wrong with a GameState class ||
     29[[br]]
     30
     31== Assertions ==
     32Last 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]]
     33When 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]]
     34An 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.
     35
     36{{{
     37void fooBar(myClass* ptr)
     38{
     39    ...
     40    ptr->aMemberFunction();
     41}
     42}}}
     43
     44You 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]]
     45The more asserts you insert, the easier bug tracking will be. You might even spot them before they could ever be triggered. [[br]][[br]]
     46'''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]]
     47
     48Usage: '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]]
     49The example above would then read:
     50
     51{{{
     52void fooBar(myClass* ptr)
     53{
     54    ...
     55    OrxAssert(ptr != 0, "You screwed the wrong pointer!");
     56    ptr->aMemberFunction();
     57}
     58}}}