New crash() function (was ? 0/1 to throw an error?)

new topic     » topic index » view thread      » older message » newer message

We now have a new crash() function. I will past code for rfind_from() from Larry
Miller to illustrate it's use:

global function rfind_from(object x, sequence s, integer start)
    integer len

    len=length(s)

    if (start > len) or (len + start < 1) then
        crash("third argument of rfind_from() is out of bounds (%d)", {start})
    end if
    -- rest of function
end function


Previously the crash() line of code read something like:

printf(1, "third argument of rfind_from() is out of bounds (%d)", {start})
? 1/0


This triggered a crash, but the real message was printed to the screen, which in
many instances a user may not see the error message (i.e. GUI or server
application running on no console). Further, when the user inspects the ex.err
file, it would state the program crashed due to a divide by zero error.

Now, the programmer can cause a real crash with a real crash message using the
above crash() routine. It is noted in the documentation and I'll note it here as
well, this should not be used for general errors. For instance, trying to open a
file that does not exist should return an error code. It should not call the
crash method as a crash cannot realistically be recovered from.

An example of when legitimate crashes take place is the find_from method:

find_from(65, "", 150)


The starting index is beyond the length of the string. This is one example of
how Euphoria may crash as is. In the above rfind_from() function, it mimics the
find_from() error code and crash condition.

Basically, uses of ? 1/0 that you see in current code should be replaced with
the new crash() method, but use it only when you cannot recover from the error.

Docs for the new crash() function can be found right now at:

http://jeremy.cowgar.com/euphoria/lib_c_d.htm#crash

Again, a temporary, non-official URL. Information you find there will most
certainly be changed before final release, it is generated from the active
development of SVN trunk.

--
Jeremy Cowgar
http://jeremy.cowgar.com

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu