Hi Mike,
The equivalent to the C# "catch" statement looks like following:

1
2
3
4
5
6
7
8
 
try
  raise (new System.InvalidOperationException("Testing..."))
with
  | :? System.InvalidOperationException as inv ->
    printf "Invalid operation: %s" inv.Message
  | _ ->
    printf "Other error";;

This example first handles "InvalidOperationException" as a special case and then handles any other error in a second block. Note that the syntax is similar to a pattern matching syntax used with the "match" construct (and you can use other pattern matching constructs in the "with" block as well).

Regarding the second question - there is "exit" function which I believe will do what you need. However, I wouldn't recommend using it in other than console applications - for example for Windows Forms, you'll typically prefer to close the main form programatically using "Close" method.

By on 11/15/2007 8:14 AM ()

I tried exit previous to posting. Let me try again and see what I can come up with.

By on 11/15/2007 9:02 AM ()

OK exit is working. One last question please on this topic.

Can you tell me why if I omit the exit 0 from below the compiler complains:

'The expression has type unit but here is used with type string'

Is it because exit returns type 'a (I am assuming)?

let d =

try

Environment.GetCommandLineArgs().[1]

with

| _ -> print_string "Directory argument missing!"

exit 0

I have only been working on learning F# for about a week and a half now so sorry if the questions are a little basic.

By on 11/15/2007 9:24 AM ()

Hi,
what you're encountering here is that in F# everything is an expression (which takes some time to understand, especially if your background is C#). In case of try..with you need to have the same return type in both branches (in a branch inside the try block and in a branch that is called when exception occurs).

In your example (without the call to exit) the try branch has a return type "string" and the with branch has a return type "unit" (meaning that no value is returned) and this confuses the F# compiler, because it doesn't know whether the whole try..with block returns a string or nothing (unit).

Exit fixes the problem, because it has a return type 'a (generic type argument), which can be instantiated to any concrete type - including the 'string', which is what you needed - this is OK, because exit terminates the application, so it never returns a value, but it can be used as if it has any return type.

If you don't want to use "exit" you have to return some valid value (or throw another exception):

1
2
3
4
5
6
let v = 
  try
    // ...
    "ok!"
  with _ ->
    "failed!"
By on 11/15/2007 10:53 AM ()

Thats what I figured as when I said

with _ ->

print_string "Error message"

""

The compiler didn't whine.

Thanks for your help.

By on 11/15/2007 11:11 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper