@@ -81,7 +81,7 @@ and other top-level statements.
8181let evalInteraction text =
8282 fsiSession.EvalInteraction( text)
8383(**
84- The two functions take string as an argument and evaluate (or execute) it as F# code. The code
84+ The two functions each take a string as an argument and evaluate (or execute) it as F# code. The code
8585passed to them does not require `;;` at the end. Just enter the code that you want to execute:
8686*)
8787evalExpression " 42+1"
@@ -97,6 +97,49 @@ let evalScript scriptPath =
9797File.WriteAllText( " sample.fsx" , " let twenty = 10 + 10" )
9898evalScript " sample.fsx"
9999
100+ (**
101+ Catching errors
102+ ------------------
103+
104+ ``EvalExpression``, ``EvalInteraction`` and ``EvalScript`` are awkward if the
105+ code has type checking warnings or errors, or if evaluation fails with an exception.
106+ In these cases you can use ``EvalExpressionNonThrowing``, ``EvalInteractionNonThrowing``
107+ and ``EvalScriptNonThrowing``. These return a tuple of a result and an array of ``FSharpErrorInfo`` values.
108+ These represent the errors and warnings. The result part is a ``Choice<_,_>`` between an actual
109+ result and an exception.
110+
111+ The result part of ``EvalExpression`` and ``EvalExpressionNonThrowing`` is an optional ``FSharpValue``.
112+ If that value is not present then it just indicates that the expression didn't have a tangible
113+ result that could be represented as a .NET object. This siutation shouldn't actually
114+ occur for any normal input expressions, and only for primitives used in libraries.
115+ *)
116+
117+ File.WriteAllText( " sample.fsx" , " let twenty = 'a' + 10.0" )
118+ let result , warnings = fsiSession.EvalScriptNonThrowing " sample.fsx"
119+
120+ // show the result
121+ match result with
122+ | Choice1Of2 () -> printfn " checked and executed ok"
123+ | Choice2Of2 exn -> printfn " execution exception: %s " exn.Message
124+
125+ (**
126+ Gives:
127+
128+ execution exception: Operation could not be completed due to earlier error
129+ *)
130+
131+ // show the errors and warnings
132+ for w in warnings do
133+ printfn " Warning %s at %d ,%d " w.Message w.StartLineAlternate w.StartColumn
134+
135+ (**
136+ Gives:
137+
138+ Warning The type 'float' does not match the type 'char' at 1,19
139+ Warning The type 'float' does not match the type 'char' at 1,17
140+ *)
141+
142+
100143(**
101144Type checking in the evaluation context
102145------------------
0 commit comments