0
我寫了一個上傳xml文件的腳本,看看下面的代碼片段:錯誤不被逮住
open System.Xml
open System.IO
open System
type XmlErrorLoad =
| Success of XmlDocument
| FileNotFound of FileNotFoundException
| OtherException of Exception
let doc (filename:string) =
try
let file = XmlDocument()
file.Load(filename)
Success file
with
| :? FileNotFoundException as ex -> FileNotFound ex
| :? Exception as ex -> OtherException ex
let fileNotExists = doc("C:\Temp\ip2.xml")
match fileNotExists with
| Success s ->()
| FileNotFound ex -> printfn "File not found: %s" ex.Message
| OtherException ex -> printfn "Exception: %s" ex.Message
在這種情況下,ip2.xml文件不存在,它應該拋出一個錯誤FileNotFound並在屏幕上打印。
"File not found: %s" ex.Message
但我得到的消息
val fileNotExists : XmlErrorLoad =
FileNotFound
System.IO.FileNotFoundException: Could not find file 'C:\Temp\ip2.xml'.
File name: 'C:\Temp\ip2.xml'
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at System.Xml.XmlDocument.Load(String filename)
at FSI_0004.doc(String filename) in D:\f#\samples\ip.fsx:line 20