本文共 1522 字,大约阅读时间需要 5 分钟。
exception WrongSecond of int
#light// define an exception typeexception WrongSecond of int// list of prime numberslet primes = [ 2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59 ]// function to test if current second is primelet testSecond() = try let currentSecond = System.DateTime.Now.Second in // test if current second is in the list of primes if List.exists (fun x -> x = currentSecond) primes then // use the failwith function to raise an exception failwith "A prime second" else // raise the WrongSecond exception raise (WrongSecond currentSecond) with // catch the wrong second exception WrongSecond x -> printf "The current was %i, which is not prime" x // call the functiontestSecond()
#light// function to write to a filelet writeToFile() =// open a file let file = System.IO.File.CreateText("test.txt") try // write to it file.WriteLine("Hello F# users") finally // close the file, this will happen even if // an exception occurs writing to the file file.Dispose()// call the functionwriteToFile()
本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/900880 ,如需转载请自行联系原作者