Raised This Month: $32 Target: $400
 8% 

Solved About ThrowNativeError


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 02-13-2022 , 11:55   About ThrowNativeError
Reply With Quote #1

Is it fine to ThrowNativeError in a function which is called by a native?
Like here

I want to create a client validation stock which will throw native errors so i can get rid of such lines in all of my natives
Code:
    if ( !IsClientIndex( iClient ) )
        return ThrowNativeError( SP_ERROR_NATIVE, "Invalid client index %i", iClient );

    if ( !IsClientInGame( iClient ) )
        return ThrowNativeError( SP_ERROR_NATIVE, "Client %i is not in game", iClient );
Also, is it necessary to return after throwing an error?
Code:
return ThrowNativeError();
(I assume that ThrowNativeError just returns 0)
I saw some natives do it, some not.
Just a little confused
__________________

Last edited by MAGNAT2645; 02-17-2022 at 11:59. Reason: Solved
MAGNAT2645 is offline
Benoist3012
Veteran Member
Join Date: Mar 2014
Location: CWave::ForceFinish()
Old 02-16-2022 , 10:46   Re: About ThrowNativeError
Reply With Quote #2

Although not marked as such, ThrowNativeError doesn't return, so sending back its returned value (which it will never return) is unnecessary.

I'm not exactly sure why it was given a return type and not void, but my best guess is simply that it was made to mimic its C++ equivalent, and also likely because native callbacks require you to return an int (and now any). Marking ThrowNativeError's return value as an int allows you to write
PHP Code:
return ThrowNativeError(...); 
instead of
PHP Code:
ThrowNativeError(...)
return 
0/-1/1
Which, i guess could look weird for anyone trying to understand what's going on inside a plugin.

Either way, just remember that this function doesn't "return" since it always throws an error.
__________________

Last edited by Benoist3012; 02-18-2022 at 05:13.
Benoist3012 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 02-16-2022 , 12:02   Re: About ThrowNativeError
Reply With Quote #3

C++ from SourceMod
https://github.com/alliedmodders/sou....cpp#L129-L149

*edit
ThrowNativeError (and ThrowError) will throw error and abort code, you won't get return value. My guess.
__________________
Do not Private Message @me

Last edited by Bacardi; 02-16-2022 at 12:20.
Bacardi is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 02-17-2022 , 08:03   Re: About ThrowNativeError
Reply With Quote #4

I thought so. But anyway thanks for the answers.
But i'm still not sure about using ThrowNativeError inside non-native functions which are called by natives.
Like this:
Code:
stock void ThrowNativeErrorIfBadClient(int client, bool inGame = true) {
    if ( client < 1 || client > MaxClients )
        ThrowNativeError( SP_ERROR_NATIVE, "Invalid client index %i", client );

    if ( inGame && !IsClientInGame( client ) )
        ThrowNativeError( SP_ERROR_NATIVE, "Client %i is not in game", client );
}

int NTV_DoSomething(Handle plugin, int numParams) {
    int iClient = GetNativeCell( 1 );
    ThrowNativeErrorIfBadClient( iClient );

    // ...
}
__________________

Last edited by MAGNAT2645; 02-17-2022 at 08:09.
MAGNAT2645 is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 02-17-2022 , 10:53   Re: About ThrowNativeError
Reply With Quote #5

That works fine: https://github.com/SilvDev/Left4DHoo...hooks.sp#L5106
__________________
Silvers is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 02-17-2022 , 11:58   Re: About ThrowNativeError
Reply With Quote #6

Okay then, thanks.
__________________
MAGNAT2645 is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 07:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode