# \[SOLVED\] Catching exception of Lwt\_unix.socket with Lwt.catch

**URL:** <https://discuss.ocaml.org/t/solved-catching-exception-of-lwt-unix-socket-with-lwt-catch/940>\
**Category:** Learning\
**Tags:** lwt\
**Created:** [October 9, 2017, 5:38pm UTC](https://discuss.ocaml.org/t/solved-catching-exception-of-lwt-unix-socket-with-lwt-catch/940 "2017-10-09T17:38:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cedlemo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cedlemo/32/1213_2.png) [@cedlemo](https://discuss.ocaml.org/u/cedlemo)\
**Post date:** [October 9, 2017, 5:38pm UTC](https://discuss.ocaml.org/t/solved-catching-exception-of-lwt-unix-socket-with-lwt-catch/940/1 "2017-10-09T17:38:59Z")

</div>

I have the following code that doesn’t work and I am not able to find why :

```auto
let open_socket addr port =
  Lwt.catch
    (fun () ->
      let sock = Lwt_unix.socket Lwt_unix.PF_INET Lwt_unix.SOCK_STREAM 0 in
      let sockaddr = Lwt_unix.ADDR_INET (addr, port) in
      Lwt_unix.connect sock sockaddr
      >>= fun () ->
        Lwt.return sock
    )
    (function
      | Unix.Unix_error (error, fn_name, param_name) -> Lwt.return ()
          Lwt_io.eprintf "%s %s %s : unable to open socket. Exiting..." (Unix.error_message error) fn_name param_name
          >>= fun () -> Lwt.return_nil
      | e -> Lwt.fail e
    )

```

The message error with Merlin is the following

```auto
This expression has type 'a list Lwt.t but an expression was expected of type Lwt_unix.file_descr Lwt.t Type 'a list is not compatible with type Lwt_unix.file_descr

```

I have used `Lwt.catch` in a previous function without problem :

```auto
let gethostbyname name =
  Lwt.catch
    (fun () ->
      Lwt_unix.gethostbyname name
      >>= fun entry ->
        let addrs = Array.to_list entry.Unix.h_addr_list in
        Lwt.return addrs
    )
    (function
      | Not_found -> lwt_print_err (name ^ ": Host not found")
        >>= fun () -> Lwt.return_nil
      | e -> Lwt.fail e
    )

```

---

<div class="post-metadata">

**Author:** ![antron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/antron/32/62_2.png) [@antron](https://discuss.ocaml.org/u/antron)\
**Post date:** [October 9, 2017, 8:39pm UTC](https://discuss.ocaml.org/t/solved-catching-exception-of-lwt-unix-socket-with-lwt-catch/940/2 "2017-10-09T20:39:17Z")

</div>

This error is because the “successful” evaluation in the above code produces a socket promise (expression `Lwt.return sock`), i.e. an `Lwt_unix.file_descr Lwt.t`. But the exception handler produces a list promise (`Lwt.return_nil` is `Lwt.return []`). Lists and `Lwt_unix.file_descr`s are incompatible types.

I don’t know exactly what you’d like to do here, but try returning options: `Lwt.return (Some sock)` on success, and `Lwt.return_none` on failure.

Also, you can write

```auto
Lwt_unix.(socket PF_INET SOCK_STREAM 0)

```

and I recommend trying the PPX, so you can use `try%lwt` and not have to write the `| e -> Lwt.fail e`. case

---

<div class="post-metadata">

**Author:** ![cedlemo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cedlemo/32/1213_2.png) [@cedlemo](https://discuss.ocaml.org/u/cedlemo)\
**Post date:** [October 9, 2017, 9:21pm UTC](https://discuss.ocaml.org/t/solved-catching-exception-of-lwt-unix-socket-with-lwt-catch/940/3 "2017-10-09T21:21:27Z")

</div>

> Lwt.return\_nil is Lwt.return

I don’t know why I thought that `Lwt.return_nul` was `Lwt.return ()`.

> Lists and Lwt\_unix.file\_descrs are incompatible types.

Ok, now it makes sense to me.

> I don’t know exactly what you’d like to do here,

The main idea is if there is a `Unix` exception catch it

1. display an error message
2. exit the program with an appropriate error code.

> I recommend trying the PPX, so you can use try%lwt and not have to write the | e → Lwt.fail e. case  
> ok

thanks for you help.
