User input may cause exception, how to branch on exception?
In my program I have a long code that reads through a text input, finding separators, letters, and numbers,to decode into a bunch of nodes.
This will cause exceptions if the user isn't too bright or a jerk, soooo... how do I stupid proof that script? Like, if there's an exception while parsing "banana" into an int, it just stops, and runs some code that resets and clears any damage...
I read something about "try{ }", but it's not clear as to what happens when there actually is an exception, and what happens/is returned (and where??) when there isn't an exception.
Answer by NoseKills · Mar 07, 2016 at 03:40 PM
I think the c# docs about try-catch say it quite well.
Anything inside the try-block is executed until the exception happens (lines inside the try-block after the exception line are skipped). If there's no exception the catch-blocks are ignored and execution continues. If there is an exception, a catch block with parameter of the same type as the exception is searched for. If found, that catch-block is executed. If not the program crashes. Parameterless catch catches all exceptions
Whatever methods you use to parse the input, the c# api docs always mention which exceptions which methods throw so you'll get an idea of what to prepare for.
Thanks! That was exactly what I wanted to know... I have to remember to search these things as c# questions ins$$anonymous$$d of unity related ones....