Method 3 - Return ValuesPros: Fast when used with built-in types and/or constants, allow a change in the client's logic and possible cleanup Cons: Error-handling isn't mandatory, values could be confusing ExplanationBasically, we either return valid data back to the caller, or a value to indicate an error: const int divide(const int divisor, const int dividend) { if( divisor == 0 ) return( 0 ); // avoid "integer divide-by-zero" error return( dividend/divisor ); } A value of zero indicates that the function failed. Unfortunately, in this example you can also get a return value of zero by passing in zero for the divisor (which is perfectly valid), so the caller has no idea whether this function returned an error. This function is nonsensical, but it illustrates the problem of using return values for error handling. It's hard or impossible to choose error values for all functions. ConclusionReturn values are best used in conditions when there is a grey area between an error and a simple change in logic. For example, a function might return a set of bit flags, some of which might be considered erroroneous by one client, and not by the other. Return values are great for conditional logic. A function trusting a function caller to notice an error condition is like a lifeguard trusting other swimmers to notice someone who is drowning. Method 4 - LoggingSometimes you do not have access to a debugger, and logging errors to a file can be quite helpful in debugging. Declare a global log file (or use std::clog) and output text to it when an error occurs. It might also help to output the file name and line number so you can tell where the error occurred. __FILE__ and __LINE__ tell the compiler to insert the current filename and line number. You can also use log files to record messages other than errors, such as the maximum number of matrices in use or some other such data that you can't access with a debugger. Or you could output the data that caused your function to fail, etc. std::fstream is great for this purpose. If you are really clever, you could figure out some way to make your assertions and exceptions log their messages to a file. :) This provides the following benefits:
Of course, it does have some overhead so you'll have to decide whether that is offset by the benefits in your situation.
|