Benutzer:MovGP0/F♯/Exception Handling

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Exception Handling

[Bearbeiten | Quelltext bearbeiten]
// define custom exception
exception EqualityError of string

let divide x y = 
   try
      try
         // ArgumentException
         if x = 0 
         then invalidArg "x can't be zero." 
         
         // FailureException 
         if y = 0
         then failwith "y can't be zero."
         
         // custom Exception
         if x = y
         then raise (EqualityError "equal") // 'raise' throws exception
         
         x / y
      with
      | :? ArgumentException -> printfn "Error was %s" message
      | Failure message -> printfn "Error was %s" message
      | EqualityError message -> printfn "Error was %s" message
   finally
      printfn "finally"