[Mono-list] Exceptions and error codes.

Miguel de Icaza miguel@ximian.com
23 Mar 2003 22:43:26 -0500


Hello,

    You do raise interesting points.

    The problem with exceptions is that throwing and catching an
exception is an expensive operation.  Using an exception as a mechanism
to return a failure error, when failure is likely to happen is
inefficient.

    Contrast `likely to happen error' with `exceptional condition:
internal error, or unlikely error to happen'.  

    Lets consider a sample: a program that uses Int32.Parse to detect
whether an integer is available, or maybe a string command exists, and
we are parsing, say, a million records:

	for (i = 0; i < one_million; i++){
		string line = readline ();
		try {
			v = Int32.Parse (line);
			handle_numberic_argument ();
		} catch {
			ParseCommand (line);
		}
	}

    This is so bad, that you probably want to rewrite the code to
pro-actively avoid parsing things that are known not to be integers.

    It is easy to turn an error-code API into an exception-throwing API
with no performance loss.   The opposite is not possible.

Miguel