SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 24/03/2013, 04:49 PM   #1
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default try/catch error handling

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)

Last edited by Slice; 09/04/2013 at 02:06 PM.
Slice is offline   Reply With Quote
Old 24/03/2013, 05:03 PM   #2
Dan..
Gangsta
 
Join Date: Jun 2012
Location: Galati, Romania
Posts: 521
Reputation: 122
Default Re: try/catch error handling

Great job!

Integrating this piece of code with crashdetect would be awesome.
Code:
main() {
    new arr[2];
    try {
        arr[2] = 0; // throws "Array index out of bounds" exception
    } catch (e) {
        // bla bla..
    }
}
Dan.. is offline   Reply With Quote
Old 24/03/2013, 05:16 PM   #3
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: try/catch error handling

Quote:
Originally Posted by Dan.. View Post
Great job!

Integrating this piece of code with crashdetect would be awesome.
Code:
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.
Slice is offline   Reply With Quote
Old 24/03/2013, 05:34 PM   #4
Dan..
Gangsta
 
Join Date: Jun 2012
Location: Galati, Romania
Posts: 521
Reputation: 122
Default Re: try/catch error handling

Quote:
Originally Posted by Slice View Post
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.
Dan.. is offline   Reply With Quote
Old 24/03/2013, 07:37 PM   #5
Whitetiger
Huge Clucker
 
Whitetiger's Avatar
 
Join Date: Nov 2007
Posts: 424
Reputation: 156
Default Re: try/catch error handling

you'll need crashdetect for this, yeah?

and also, try/catch is good, but where is finally?
Whitetiger is offline   Reply With Quote
Old 24/03/2013, 07:41 PM   #6
Dan..
Gangsta
 
Join Date: Jun 2012
Location: Galati, Romania
Posts: 521
Reputation: 122
Default Re: try/catch error handling

Quote:
Originally Posted by whitetigerswt View Post
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.
Dan.. is offline   Reply With Quote
Old 24/03/2013, 07:46 PM   #7
Misiur
High-roller
 
Misiur's Avatar
 
Join Date: Jul 2009
Location: Poland
Posts: 2,529
Reputation: 554
Default Re: try/catch error handling

Cool, thought if it'd be possible in pawn few days ago. Will surely use
Misiur is offline   Reply With Quote
Old 24/03/2013, 08:21 PM   #8
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: try/catch error handling

@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).
Slice is offline   Reply With Quote
Old 05/04/2013, 09:35 PM   #9
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: try/catch error handling

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.
Slice is offline   Reply With Quote
Old 05/04/2013, 10:54 PM   #10
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,835
Reputation: 1607
Default Re: try/catch error handling

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).
Slice is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[GameMode] [GM]ZCops_VS_Criminals | NFS Catch system | Taxi Bots System | by Zuc [ZFM]Zuc Gamemode Scripts 139 07/10/2018 04:03 PM
[FilterScript] Catch The Circle [Clickable Textdraw] Horrible Filterscripts 50 19/06/2013 07:11 PM
[FilterScript] [FS]Catch 'n Release Mike_Peterson Filterscripts 5 05/08/2011 05:40 PM
Tips on how to catch fast (and easy) ban evaders? Claude Help Archive 2 17/08/2010 03:40 PM
Catch The Flag - CTF (you can play this in //army) F40LM Help Archive 6 13/05/2010 10:22 AM


All times are GMT. The time now is 03:41 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.