[Include] try/catch error handling
#1

This adds try/catch statements to Pawn, very similar to those in other languages.

A good usage area is commands. The player who typed the command wants to know why the command failed. Why it failed, however, could be due to an error inside a function inside nested calls.

Normally when functions fail they just return 0, -1, or something similar. While that works in many cases, sometimes you want to inform the user of where and why it went wrong.

Refer to the tutorial for more information.

Code: https://github.com/oscar-broman/exceptions (be sure to look at this topic for updates)
Reply
#2

Great job!

Integrating this piece of code with crashdetect would be awesome.
Код:
main() {
    new arr[2];
    try {
        arr[2] = 0; // throws "Array index out of bounds" exception
    } catch (e) {
        // bla bla..
    }
}
Reply
#3

Quote:
Originally Posted by Dan..
Посмотреть сообщение
Great job!

Integrating this piece of code with crashdetect would be awesome.
Код:
main() {
    new arr[2];
    try {
        arr[2] = 0; // throws "Array index out of bounds" exception
    } catch (e) {
        // bla bla..
    }
}
I thought the same right after I posted! I'm almost finished with it.

Edit: added & updated main post.
Reply
#4

Quote:
Originally Posted by Slice
Посмотреть сообщение
I thought the same right after I posted! I'm almost finished with it.

Edit: added & updated main post.
Wow, that's great! You were fast.
Reply
#5

you'll need crashdetect for this, yeah?

and also, try/catch is good, but where is finally?
Reply
#6

Quote:
Originally Posted by whitetigerswt
Посмотреть сообщение
you'll need crashdetect for this, yeah?

and also, try/catch is good, but where is finally?
You'll need the crashdetect plugin only if you want to detect runtime errors.

The `finally` keyword is probably coming in future releases.
Reply
#7

Cool, thought if it'd be possible in pawn few days ago. Will surely use
Reply
#8

@whitetiger: I didn't make it yet. I'm trying to figure out how to.


I wouldn't recommend using this to be sloppy with array indexes, zero divisions, memory allocation, stack/heap, etc. just because it will catch the errors.
Runtime errors should be fixed and avoided using sanity checking.

The main idea is this should be used to throw and catch custom errors.

Also note that runtime errors will cause the outer public to return 0, essentially. If you catch an error in OnPlayerCommandText it will print "SERVER: Unknown command". Everything else will still work, but you can't get rid of that message (unless you make a 0ms timer and do everything in that timer function).
Reply
#9

Major update. I rewrote the include twice.

It now supports return statements, nested try/catch statements (not in the same function), and finally.

If you return something in try or catch, it will execute finally then return the original value. Unless, of course, if you return something different in finally.

It's almost ready for production use - I still have some minor things to add, and lots of testing to do.
Reply
#10

Adding __r in the beginning is what prevented it from optimizing the OR operations (I'm also surprised it did).

I didn't add the runtime error exceptions in the latest version because of some limitations. I suspect crashdetect has to be modified for a near-perfect implementation of it (issue).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)