About Monkey 2 › Forums › Monkey 2 Programming Help › try/catch & exceptions handling basics
Tagged: try catch exceptions
This topic contains 3 replies, has 4 voices, and was last updated by
EdzUp
1 year, 11 months ago.
-
AuthorPosts
-
May 12, 2017 at 9:56 am #8173
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]Monkey12345678910111213141516171819202122232425262728[crayon-5cba166f5278f090241726 inline="true" ]Namespace myapp#Import "<std>"Using std..Class divByZeroEx Extends ThrowableField msg:StringMethod New (mesag:String)Self.msg = mesagEndEndFunction CheckDivideByZero:Float (a:Float,b:Float)If b=0 Then Throw New divByZeroEx ("Division by zero detected")Return a/bEndFunction Main()Local i:=5Local j:=0TryPrint CheckDivideByZero(i,j)Catch err:divByZeroExPrint err.msgEndEndIt 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?
May 15, 2017 at 12:19 am #8179There’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.
May 15, 2017 at 12:37 am #8180In 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.
May 15, 2017 at 7:08 pm #8185Debug 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.
-
AuthorPosts
You must be logged in to reply to this topic.