F# How to catch all exceptions

核能气质少年 提交于 2021-01-23 06:25:31

问题


I know how to catch specific exceptions as in the following example:

let test_zip_archive candidate_zip_archive =
  let rc =
      try
        ZipFile.Open(candidate_zip_archive.ToString(), ZipArchiveMode.Read) |> ignore
      zip_file_ok
      with
      | :? System.IO.InvalidDataException -> not_a_zip_file
      | :? System.IO.FileNotFoundException -> file_not_found         
      | :? System.NotSupportedException -> unsupported_exception
  rc

I am reading a bunch of articles to see if I can use a generic exception in the with, like a wildcard match. Does such a construct exist, and, if so, what is it?


回答1:


I like the way that you applied the type pattern test (see link). The pattern you are looking for is called the wildcard pattern. Probably you know that already, but didn't realise that you could use it here, too. The important thing to remember is that the with part of a try-catch block follows match semantics. So, all of the other nice pattern stuff applies here, too:

  • https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/pattern-matching

Using your code (and a few embellishments so that you can run it in FSI), here's how to do it:

#r "System.IO.Compression.FileSystem.dll"
#r "System.IO.Compression.dll"

open System  
open System.IO
open System.IO.Compression

type Status = 
  | Zip_file_ok
  | Not_a_zip_file
  | File_not_found
  | Unsupported_exception
  | I_am_a_catch_all

let test_zip_archive candidate_zip_archive =
  let rc() =
    try
      ZipFile.Open(candidate_zip_archive.ToString(), ZipArchiveMode.Read) |> ignore
      Zip_file_ok
    with
    | :? System.IO.InvalidDataException -> Not_a_zip_file
    | :? System.IO.FileNotFoundException -> File_not_found         
    | :? System.NotSupportedException -> Unsupported_exception
    | _ -> I_am_a_catch_all // otherwise known as "wildcard"
  rc



回答2:


Yes, you can do it like this:

let foo = 
  try
    //stuff (e.g.)
    //failwith "wat?"
    //raise (System.InvalidCastException())
    0 //return 0 when OK
  with ex ->
    printfn "%A" ex //ex is any exception 
    -1 //return -1 on error

This is the same as C#'s catch (Exception ex) { }

To discard the error you can do with _ -> -1 (which is the same as C#'s catch { })



来源:https://stackoverflow.com/questions/44057709/f-how-to-catch-all-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!