如何处理IO::Socket::INET中的异常?

zen*_*nix 5 raku

假设本地主机中没有应用程序侦听端口 12340。

下面的命令不应该打印“error”吗?

$ raku -e "IO::Socket::INET.new(:host('localhost'), :port(12340)) or die 'error'"
Could not connect socket: No connection could be made because the target machine actively refused it.

  in block <unit> at -e line 1
Run Code Online (Sandbox Code Playgroud)

Eli*_*sen 4

的当前实现nqp::connect(建立连接的底层逻辑)会引发异常X::AdHoc。如果无法连接,IO::Socket::INET.new则返回对我来说确实更有意义。Failure

我创建了一个拉取请求来创建此行为。

在此之前,您可以在本地使用相同的代码更改:

sub connect($host, $port) {
    CATCH { return .Failure }
    IO::Socket::INET.new(:$host, :$port)
}

connect('localhost', 12340) or die 'error';
Run Code Online (Sandbox Code Playgroud)

  • 我使用了 `try` 和 `if $!`,它对我来说效果很好......但我想获得错误号(Windows 中为 1225,Linux 中为 111),但找不到方法来做到这一点 (3认同)