Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 3 and Version 4 of code/doc/Exception


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

Legend:

Unmodified
Added
Removed
Modified
  • code/doc/Exception

    v3 v4  
    1 = Exceptions, Assertions and COUT =
    2 Whenever something unwanted happens, the programmer needs to react in a certain way.[[br]]
    3 We 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]]
     1= Exceptions =
     2This article assumes that you know what an Exception is and how to throw it. Otherwise, see [http://en.wikipedia.org/wiki/Exception_handling wikipedia] or [http://www.cplusplus.com/doc/tutorial/exceptions.html cpluscplus.com]. [[br]][[br]]
    83
    9 == COUT(#), displays messages ==
    10 Whenever 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 ==
    15 This 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]]
    16 When 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]]
     4When 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 up to the programmer to display the message 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]]
    175
    186To 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]]
     
    2816|| GameState || Something went wrong with a GameState class ||
    2917[[br]]
    30 
    31 == Assertions ==
    32 Last 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]]
    33 When 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]]
    34 An 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 {{{
    37 void fooBar(myClass* ptr)
    38 {
    39     ...
    40     ptr->aMemberFunction();
    41 }
    42 }}}
    43 
    44 You 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]]
    45 The 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 
    48 Usage: '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]]
    49 The example above would then read:
    50 
    51 {{{
    52 void fooBar(myClass* ptr)
    53 {
    54     ...
    55     OrxAssert(ptr != 0, "You screwed the wrong pointer!");
    56     ptr->aMemberFunction();
    57 }
    58 }}}