www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Howto catch SocketOSException?

reply Jolly James <j.j jmail.com> writes:
How do you catch an std.socket.SocketOSException?


The following does not work, as the exception occurs anyway and 
leads to a crash:

import ae.net.asockets;

void main(string[] args)
{
	TcpServer tcp = new TcpServer();

	try
	{
		tcp.listen(2345, "127.0.0.1c");
		// '...c' makes the IP address invalid
	}
	catch (std.socket.SocketOSException e)
	{
		return;
	}
	catch (Exception e)
	{
		return;
	}

	socketManager.loop();
}
Output:
std.socket.SocketOSException std\socket.d(975): getaddrinfo 
error: Unknown Host
Mar 25 2017
parent reply bauss <jj_1337 live.dk> writes:
On Sunday, 26 March 2017 at 00:34:03 UTC, Jolly James wrote:
 How do you catch an std.socket.SocketOSException?


 The following does not work, as the exception occurs anyway and 
 leads to a crash:

import ae.net.asockets;

void main(string[] args)
{
	TcpServer tcp = new TcpServer();

	try
	{
		tcp.listen(2345, "127.0.0.1c");
		// '...c' makes the IP address invalid
	}
	catch (std.socket.SocketOSException e)
	{
		return;
	}
	catch (Exception e)
	{
		return;
	}

	socketManager.loop();
}
Output:
std.socket.SocketOSException std\socket.d(975): getaddrinfo 
error: Unknown Host
This part: catch (std.socket.SocketOSException e)
	{
		return;
	}
Is redundant, because SocketOSException inherits SocketException which inherits Exception. It should already be caught by catch (Exception e) You should have a full stacktrace, chances are that it's invoked in your loop()?
Mar 25 2017
parent reply Jolly James <j.j jmail.com> writes:
On Sunday, 26 March 2017 at 01:22:24 UTC, bauss wrote:
 On Sunday, 26 March 2017 at 00:34:03 UTC, Jolly James wrote:
[...]
This part: catch (std.socket.SocketOSException e)
[...]
[...]
I know that inheritance stuff, but none (!) of them catches that strange exception either. You can ignore the loop()-method. It is not called as the application will never reach this statement, because it cannot, because it crashes already in the listen()-method in consequence of the exception that does not get caught by the try-catch block.
std.socket.SocketOSException std\socket.d(975): getaddrinfo 
error: Unknow host.
----------------
	0x004205BE in pure  safe bool 
std.exception.enforce!(bool).enforce(bool, lazy object.Throwable)
	0x0040D3A2 in  trusted std.socket.AddressInfo[] 
std.socket.getAddressInfo!(immutable(char)[], 
std.socket.AddressInfoFlags, std.socket.SocketType, 
std.socket.ProtocolType).getAddressInfo(const(char[]), 
immutable(char)[], std.socket.AddressInfoFlags, 
std.socket.SocketType, std.socket.ProtocolType).__lambda7()
	0x0040D37B in  safe std.socket.AddressInfo[] 
std.socket.getAddressInfo!(immutable(char)[], 
std.socket.AddressInfoFlags, std.socket.SocketType, 
std.socket.ProtocolType).getAddressInfo(const(char[]), 
immutable(char)[], std.socket.AddressInfoFlags, 
std.socket.SocketType, std.socket.ProtocolType) at 
C:\dlang\dmd2\windows\bin\..\..\src\phobos\std\socket.d(945)
	0x00404DAF in ushort ae.net.asockets.TcpServer.listen(ushort, 
immutable(char)[]) at C:\Users\jolly\src\ae\net\asockets.d(1242)
	0x00416749 in _Dmain at C:\Users\jolly\src\app.d(48)
	0x00423597 in 
D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv
	0x0042355B in void rt.dmain2._d_run_main(int, char**, extern 
(C) int function(char[][])*).runAll()
	0x0042345C in _d_run_main
	0x00417D74 in main at C:\Users\jolly\src\ae\net\asockets.d(7)
	0x00443EBD in mainCRTStartup
	0x757362C4 in BaseThreadInitThunk
	0x773C0FD9 in RtlSubscribeWnfStateChangeNotification
	0x773C0FA4 in RtlSubscribeWnfStateChangeNotification
↑ stacktrace
Mar 25 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 26 March 2017 at 02:24:56 UTC, Jolly James wrote:
 You can ignore the loop()-method. It is not called as the 
 application will never reach this statement, because it cannot, 
 because it crashes already in the listen()-method in 
 consequence of the exception that does not get caught by the 
 try-catch block.
Try putting it in the try anyway and see what happens. It is an async socket library, they can do weird things.\
Mar 25 2017
parent reply Jolly James <j.j jmail.com> writes:
On Sunday, 26 March 2017 at 02:41:46 UTC, Adam D. Ruppe wrote:
 On Sunday, 26 March 2017 at 02:24:56 UTC, Jolly James wrote:
 You can ignore the loop()-method. It is not called as the 
 application will never reach this statement, because it 
 cannot, because it crashes already in the listen()-method in 
 consequence of the exception that does not get caught by the 
 try-catch block.
Try putting it in the try anyway and see what happens. It is an async socket library, they can do weird things.\
Unfortunately not working either. I should not forget to mention that the exception also raises when the code does not contain the loop()-call.
Mar 26 2017
parent reply Jolly James <j.j jmail.com> writes:
On Sunday, 26 March 2017 at 11:35:00 UTC, Jolly James wrote:
 On Sunday, 26 March 2017 at 02:41:46 UTC, Adam D. Ruppe wrote:
 On Sunday, 26 March 2017 at 02:24:56 UTC, Jolly James wrote:
 You can ignore the loop()-method. It is not called as the 
 application will never reach this statement, because it 
 cannot, because it crashes already in the listen()-method in 
 consequence of the exception that does not get caught by the 
 try-catch block.
Try putting it in the try anyway and see what happens. It is an async socket library, they can do weird things.\
Unfortunately not working either. I should not forget to mention that the exception also raises when the code does not contain the loop()-call.
Found out something: You cannot catch any exception thrown in the listen()-method in general. ■ Original code:
auto addressInfos = getAddressInfo(addr, to!string(port), 
AddressInfoFlags.PASSIVE, SocketType.STREAM, ProtocolType.TCP);
■ Modified one:
AddressInfo[] addressInfos;

try
{
	addressInfos = getAddressInfo(addr, to!string(port), 
AddressInfoFlags.PASSIVE, SocketType.STREAM, ProtocolType.TCP);
}
catch(SocketOSException e)
{
	throw new Exception("Invalid address: " ~ addr, e);
}
■ Not working try-catch:
	try
	{
		tcp.listen(2345, "127.0.0.1c");
		socketManager.loop();
	}
	catch (Exception e)
	{
		return;
	}
Mar 26 2017
parent reply bauss <jj_1337 live.dk> writes:
On Sunday, 26 March 2017 at 11:46:39 UTC, Jolly James wrote:
 On Sunday, 26 March 2017 at 11:35:00 UTC, Jolly James wrote:
 [...]
Found out something: You cannot catch any exception thrown in the listen()-method in general. ■ Original code:
[...]
■ Modified one:
[...]
■ Not working try-catch:
[...]
Chances are it's invoked in another thread and thus you can't catch it like that. To sum it up. Ex. void thisFunctionThrows() { ... } void ableToCatch() { try { thisFunctionThrows(); } catch (Exception e) { // We can catch the exception ... } } void notAbleToCatch() { try { spawn(&thisFunctionThrows); } catch (Exception e) { // We cannot catch the exception ... } } void ableToCatchToo() { spawn(&ableToCatch); // We're able to handle the exception, because the try/catch is handled in the thread that calls the function that throws. }
Mar 26 2017
parent reply Jolly James <j.j jmail.com> writes:
On Sunday, 26 March 2017 at 18:50:13 UTC, bauss wrote:
 On Sunday, 26 March 2017 at 11:46:39 UTC, Jolly James wrote:
[...]
Chances are it's invoked in another thread and thus you can't catch it like that. To sum it up. Ex. void thisFunctionThrows() { ... } void ableToCatch() { try { thisFunctionThrows(); } catch (Exception e) { // We can catch the exception ... } } void notAbleToCatch() { try { spawn(&thisFunctionThrows); } catch (Exception e) { // We cannot catch the exception ... } } void ableToCatchToo() { spawn(&ableToCatch); // We're able to handle the exception, because the try/catch is handled in the thread that calls the function that throws. }
If you want try to help me, mabye this helps you: https://github.com/CyberShadow/ae/blob/master/net/asockets.d#L1237
Mar 26 2017
parent Jolly James <j.j jmail.com> writes:
On Sunday, 26 March 2017 at 21:57:29 UTC, Jolly James wrote:
 On Sunday, 26 March 2017 at 18:50:13 UTC, bauss wrote:
 [...]
If you want try to help me, mabye this helps you: https://github.com/CyberShadow/ae/blob/master/net/asockets.d#L1237
Finally found the bug: I had a logical error in the way how I used this code.
Mar 27 2017