# Lwt\_io catching exception issue

**URL:** <https://discuss.ocaml.org/t/lwt-io-catching-exception-issue/7702>\
**Category:** Learning\
**Tags:** lwt, exceptions\
**Created:** [April 19, 2021, 8:38am UTC](https://discuss.ocaml.org/t/lwt-io-catching-exception-issue/7702 "2021-04-19T08:38:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maiste](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/maiste/32/3919_2.png) [@maiste](https://discuss.ocaml.org/u/maiste)\
**Post date:** [April 19, 2021, 8:38am UTC](https://discuss.ocaml.org/t/lwt-io-catching-exception-issue/7702/1 "2021-04-19T08:38:25Z")

</div>

Hi,  
I’m trying to execute `ls` on a distant machine using `Lwt_process`, `Lwt_io` and `Lwt_stream`.  
The code to read the directory is the following:

```ocaml
  let readdir_aux ~remote dir =
      let cmd = Format.sprintf "ls -1 %s" dir in
      let ssh_cmd = create_ssh_cmd remote cmd in
      let lines =
        Lwt_process.pread_lines
          ~timeout:10.0
          ~stdin:`Dev_null
          ~stderr:`Dev_null
          ssh_cmd
      in
      Lwt_main.run @@
      (Lwt_stream.to_list lines
      >>= fun lines ->
      List.filter (fun line -> line <> "") lines
      |> Array.of_list |> Lwt.return)

```

When I run the code with no `try-with`, I have absolutely no issue:

```ocaml
let () = 
    Sys.readdir_aux ~remote "/tmp/tryme/"
    |> Array.iter (fun x -> Format.printf "-%s\n" x)

```

But, if I had a try catch to surround the call, it just stops:

```ocaml
let () =
try
    Sys.readdir_aux ~remote "/tmp/tryme/"
    |> Array.iter (fun x -> Format.printf "-%s\n" x)
with _ -> Format.printf "[OK] Exception readdir@."

```

Does someone already have this issue ? I can’t see what I missed…

---

<div class="post-metadata">

**Author:** ![maiste](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/maiste/32/3919_2.png) [@maiste](https://discuss.ocaml.org/u/maiste)\
**Post date:** [April 19, 2021, 1:39pm UTC](https://discuss.ocaml.org/t/lwt-io-catching-exception-issue/7702/2 "2021-04-19T13:39:21Z")

</div>

It appears that the error came from the fact, that I didn’t close the process. Closing the process resolved the problem.
