Try/EndTry command?
BlitzMax Forums/BlitzMax Beginners Area/Try/EndTry command?
| ||
I don't know why the "Try" command exist. isn't more easy to use "exit" and declare an extra variable? Strict Local a:Int Try Repeat a:+1 Print a If a>20 Throw "chunks" Forever Catch a$ Print "caught exception "+a$ EndTry Strict Local a:Int Local b:string Repeat a:+1 Print a If a>20 Then b="chunks"; Exit Forever If b then Print "caught exception "+b |
| ||
try is needed when you don't know ALL the reason that can raise an error...is a security door for the programmer. |
| ||
Not to say there is a fundamental difference: It works across funciton calls. If function A calls function B whih calls Functions C, A can catch an exception raised by C. It's a structured ways of handling errors, that doesn't require to clutter your code with error checking at each and every place an error can occur (with exception you can handle the error at the place you find the more appropriate, which can be way down in the call chain). Your sample code is just a special case, exception handling allows much more. You'll find some more information by just searching the forum for "exception". |
| ||
isn't more easy to use "exit" and declare an extra variable? No. Exit and Return are designed for normal program flow, where as exception handling is for abnormal (ie. something went wrong) program flow.An excellent way to use exceptions is for an automated unit test framework (like JUnit), where your tests throw an exception, that the Framework picks up on, thus signifying a test failure. |
| ||
Thanks for the reply. I need to learn more about it. |