Raised This Month: $51 Target: $400
 12% 

Try and Catch in Sourcepawn


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DJPlaya
Senior Member
Join Date: Nov 2014
Location: Germany
Old 02-25-2018 , 13:17   Try and Catch in Sourcepawn
Reply With Quote #1

Basically we can spam with ifs and elses but will there ever be try and catch in Sourcepawn?
__________________
My biggest Projects: Kigen AC Redux, Forlix Floodcheck Redux
DJPlaya is offline
Send a message via Skype™ to DJPlaya
Mitchell
~lick~
Join Date: Mar 2010
Old 02-25-2018 , 21:40   Re: Try and Catch in Sourcepawn
Reply With Quote #2

Provide an instance where you would want to catch an exception? Everything that would fail a plugin can be guarded against already and if it isnt then you should report it.
Mitchell is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 02-26-2018 , 00:45   Re: Try and Catch in Sourcepawn
Reply With Quote #3

Try Catch statements are for lazy coders.

Dont be lazy. Code in your checks where required to avoid exceptions.
__________________
Neuro Toxin is offline
DJPlaya
Senior Member
Join Date: Nov 2014
Location: Germany
Old 02-26-2018 , 10:00   Re: Try and Catch in Sourcepawn
Reply With Quote #4

I know, but it would make every Error Check some lines less
An Example SocketCreate, we could try it and wouldent need that nasty Error Callback and could catch it instead

Code:
public LetsConnect(Handle Timer)
{
	g_hSocket = SocketCreate(SOCKET_TCP, Network_OnSocketError);
}

public Network_OnSocketError(Handle socket, const errorType, const errorNum, any client)
{
	if(socket == INVALID_HANDLE)
		return;
		
	if(g_hSocket == socket)
		g_hSocket = INVALID_HANDLE;
		
	CloseHandle(socket);
	CreateTimer(10, LetsConnect, _, TIMER_HNDL_CLOSE); // Lets Retry
}
Instead we could
Code:
public LetsConnect(Handle Timer)
{
	try
		g_hSocket = SocketCreate(SOCKET_TCP, Network_OnSocketError);
		
	catch
	{
		if(socket == INVALID_HANDLE)
			return;
			
		if(g_hSocket == socket)
			g_hSocket = INVALID_HANDLE;
		
		CloseHandle(socket);
		CreateTimer(10, LetsConnect, _, TIMER_HNDL_CLOSE); // Lets Retry
	}
}

public Network_OnSocketError(Handle socket, const errorType, const errorNum, any client)
{
	// DO ONOZ
}
__________________
My biggest Projects: Kigen AC Redux, Forlix Floodcheck Redux

Last edited by DJPlaya; 02-26-2018 at 10:03.
DJPlaya is offline
Send a message via Skype™ to DJPlaya
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-26-2018 , 10:24   Re: Try and Catch in Sourcepawn
Reply With Quote #5

The error callback is there because that API is asynchronous (the error may not be available instantly), try/catch can't help you there.
__________________

Last edited by klippy; 02-26-2018 at 10:25.
klippy 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:22.


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