try/catch & exceptions handling basics

About Monkey 2 Forums Monkey 2 Programming Help try/catch & exceptions handling basics

This topic contains 3 replies, has 4 voices, and was last updated by  EdzUp 1 year, 11 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #8173

    abakobo
    Participant

    I’d like to have some better knowledge of the exceptions handling…

    I wrote this little example as a first test with try/catch

    [/crayon]

    It works fine.. BUT is it how it is supposed to be used?

    Also if I put “Print i/j” in the Try section it looks like it throws an exception but I can’t catch it. What would be the name of a throwable object for a “Floating point exception”? Are there any already existing throwables in core mx2?

    #8179

    Mark Sibly
    Keymaster

    There’s currently no way to trap integer divide by 0 exceptions in monkey2 – you should alwys check for them in code if there’s a possiblity an int divide by 0 could happen.

    Note this only applies to int divide by 0, for a floating point divide you’ll get +/- infinity. Ditto in floating point any expression with a ‘nan’ in it will produce nan, as will 0.0/0.0 (and probably some other meaningless expressions like infinity/infinity etc.). So if you change i and j to floats (they’re currently ints), Print i/j should print ‘infinity’ or something.

    Note there’s really 2 kinds of exceptions being discussed here: cpu/hard exceptions, which are caused by the cpu when Something Bad Happens (eg: int div by 0, invalid mem access, privelage violation etc) and software exceptions which are a more general purpose flow control thing and supported by monkey2 via throw/catch, eg: the mx2cc compiler uses software exceptions to flag errors in the parse, semant etc phases. So while you can say the cpu ‘throws’ exceptions, the mechanism is different and it general happens at a lower OS/C++ level.

    You cannot currently ‘catch’ cpu exceptions in monkey2, and may never be able to. Mechanisms to convert cpu exceptions to software exceptions differ all over the place, and I have never liked the idea of NOT aborting an app that has caused an illegal memory access or privelage violation or something.

    #8180

    Simon Armstrong
    Participant

    In blitz I would expect debug mode to check for integer divide by zero protecting the developer from low level crash and making it quite clear it is their fault.

    #8185

    EdzUp
    Participant

    Debug mode has to catch div0 errors, even if its a simple check to see if the integer is 0 before a divide takes place it would be handy. Normally I always code to check for divide by 0 as its just me (harkens from my C days) but not all coders are so stringent.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.