2015-02-12 36 views
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 

回答

1

我试过你的代码作为F#交互式输入,并获得所需的信息

File not found: Could not find file 'C:\Temp\ip2.xml'. 

除了输出F#互动名单代码中定义的所有值,所以我还得到以下印刷品:

type XmlErrorLoad = 
    | Success of XmlDocument 
    | FileNotFound of FileNotFoundException 
    | OtherException of Exception 
val doc : filename:string -> XmlErrorLoad 
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_0003.doc(String filename) 
val it : unit =() 

您只在F#Interactive中获得此输出,作为REPL反馈的一部分。在一个正常的程序中,这个输出不会被生成。