dpnof28482 2017-07-20 18:16
浏览 545
已采纳

golang-阻止应用退出

In Go, with a panic() you can use defer and recover() to prevent an app from exiting and continue executing code.

However, I'm trying to prevent my app from exiting when getting a dial tcp 192.168.1.1:830: getsockopt: connection refused. The application Exits with a status code of 1. It's technically not an error so I can't catch the error. The external package I'm using to make the tcp dial causes the app to Exit when this condition occurs. (In this case, it's because the port is blocked.)

So how can I recover the Exit from another package and continue on with my application? Take the below as an example:

func makeRequest(target string) {
    // Exits with status code 1, if connection refused 
    res, err := request.NewSession(target)
}
  • 写回答

1条回答 默认 最新

  • doudieyou5209 2017-07-21 00:20
    关注

    Unfortunately no, you can't recover from a call to os.Exit(). The documentation says that it exits immediately, and not even differed functions are called. I recommend to not use a package if it has an exit in it, as that is a pretty bad design.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码