Re: Crash or return forbidden value?
- Posted by Jeremy Cowgar <jeremy at cowgar?com> May 08, 2008
- 658 views
Shawn Pringle wrote: > > What you are talking about here is building an Exception system into the > interpreter. It would be analogous to try/throw/catch and Exceptions like > in Java or C++. Perhaps exceptions could be less invasive than adding C++ > syntax if say some new builtin routines such as a register_exception_routine > were added. The problem with this is the exception routine knows nothing about what function function caused the problem. You could pass a function name, error, etc... but that still does not help. Nor does it give the ability to recover from anything. For instance:
function exception_handler(sequence name, sequence msg, integer error) ... end function regisert_handler(...) fname = "abc.txt" f = open(fname, "r") -- abc.txt does not exist, lets say
Now, what is exception_handler suppose to do? It does not know if the app can recover or how it should recover. However, take this example:
try f = open(fname, "r") catch ex if ex[CODE] = ERR_FILE_NOT_EXIST then f = open(fname, "w") puts(f, "# File created today\n") else crash("Could not handle exception: %i %s", {ex[CODE], ex[MSG]}) end if end try
So... that takes more coding, but it's useful and can do something about the exception. Sure, the example was not that good as we can handle that now, but that's the idea. Ok, an exception system will take a lot of planning and a lot of work. What we are trying to solve right now what error condition should return an error code and what error condition should cause a crash? Many times in Euphoria code, I see ? 1/0 which the programmer feels is an error that cannot be recovered from, therefore causes a crash to terminate Euphoria with a ex.err file. We now have a better way of doing that in 4.0. You can:
crash("Fatal error I cannot recover from. Error in hard drive: %s", {hard_drive_name})
So, this keeps a true stack trace, the user is given a realistic error message (not a divide by zero message), etc... But, the question is, when to use crash() vs. return an error code. For instance, when opening a file if it does not exist an error code is returned vs. s="Hello" ? s[500] ... an index out of bounds causes a crash. -- Jeremy Cowgar http://jeremy.cowgar.com