0

As I'm debugging, I noticed $exception within the locals window as a result of an exception thrown implying this variable is available in the scope.

The question is how can I access this $exception variable.

How can line 78 be?

| x ->  Debug.WriteLine("exception" + $exception.Response.StatusCode)

enter image description here

App2015
  • 973
  • 1
  • 7
  • 19

1 Answers1

4

You can catch exceptions of a specific type with this syntax:

try
  ...
with
| :? System.Net.WebException as e ->
    let response = e.Response :> System.Net.HttpWebResponse
    Console.WriteLine("exception" + response.StatusCode)

Note you need to cast WebException.Response to System.Net.HttpWebResponse so you can access its StatusCode property.

See the MSDN docs for more.

Taylor Wood
  • 15,886
  • 1
  • 20
  • 37
  • It might be worth pointing out that the case to `HttpWebResponse` can fail: https://stackoverflow.com/questions/3614034/system-net-webexception-http-status-code – DaveShaw Mar 18 '18 at 22:52