Author Topic: Does "Catch" catch all exceptions?  (Read 6688 times)

0 Members and 1 Guest are viewing this topic.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Does "Catch" catch all exceptions?
« on: February 24, 2012, 04:26:31 AM »
Lately I found a bug in my program (or a wrong assumption). I use a function to read data from a web location, via http.

Code: [Select]
Function ReadItemsFromWeb() As Boolean
    Try
      Dim uri As New System.Uri(My.Settings.sCCTBWebLocation)
      Dim request As Net.WebRequest = Net.WebRequest.Create(uri)
      request.Proxy = Net.WebRequest.DefaultWebProxy
      request.Credentials = Net.CredentialCache.DefaultCredentials

      If Not request.GetResponse Is Nothing Then
        Dim response As Net.HttpWebResponse = CType(request.GetResponse(), Net.HttpWebResponse)
        Dim dataStream As IO.Stream = response.GetResponseStream()
        Dim reader As New IO.StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        If Not responseFromServer.Trim = "" Then
          My.Settings.sSettingCCTBNewItems = responseFromServer
          My.Settings.iSettingCCTBLastCheck = DateSince()
          My.Settings.Save()
        End If
        response.Close()
        reader.Close()
        dataStream.Close()
        request = Nothing
        Return True
      End If
    Catch ex as Exception

    End Try
    Return False
  End Function

I thought the Catch would catch every error, because Exception is generic. But the function resulted in an error when there was a proxy error, or no internet connection. The function did work when I catched the ex as Net.WebException.

Why did the Catch ex as Exception not catch the WebException?

I also can't use this, because a WebException is not an ordinary Exception:

Code: [Select]
Catch ex As Exception
      If TypeOf ex Is Net.WebException Then

      End If

There is no general way to catch every exception? Or is this an exception where I have to catch every possible exceptions (WebException, HttpListenerException, CookieException)?
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

kaefer

  • Guest
Re: Does "Catch" catch all exceptions?
« Reply #1 on: February 24, 2012, 04:54:44 AM »
Code: [Select]
Catch ex As Exception
      If TypeOf ex Is Net.WebException Then

      End If

You didn't write if any AutoCAD libraries were involved. If they were, check that you do not inadvertently catch Autodesk.AutoCAD.Runtime.Exception.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Does "Catch" catch all exceptions?
« Reply #2 on: February 24, 2012, 05:30:30 AM »
Exception is indeed Autodesk.AutoCAD.Runtime.Exception.

Shouldn't I use that?
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Does "Catch" catch all exceptions?
« Reply #3 on: February 24, 2012, 05:44:31 AM »
not if you're trying (anticipating) to catch a
System.Net.WebException
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Does "Catch" catch all exceptions?
« Reply #4 on: February 24, 2012, 11:03:58 AM »
You can also have multiple catches

http://support.microsoft.com/kb/315965#

You should be aware that asynchronous http requests exceptions are not always caught. I've had some at length discussion with microsoft support concerning this when I was developing a desktop application with a web component. The error was eventually found in the .net libraries, but they have still not addressed it.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Does "Catch" catch all exceptions?
« Reply #5 on: February 24, 2012, 01:37:03 PM »
not if you're trying (anticipating) to catch a
System.Net.WebException

I was not aware of WebExceptions are not catched in a general Exception. In this function I am not really interested in why there is no internet connection but catch any error to be able to continue without an internet connection. Now I still got a runtime error, and because my application implements IExtensionApplication, I did not see the error. The application just didn't run. AutoCAD killed the process silently  :x
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Does "Catch" catch all exceptions?
« Reply #6 on: February 24, 2012, 01:39:59 PM »
You can also have multiple catches

http://support.microsoft.com/kb/315965#

You should be aware that asynchronous http requests exceptions are not always caught. I've had some at length discussion with microsoft support concerning this when I was developing a desktop application with a web component. The error was eventually found in the .net libraries, but they have still not addressed it.

I know there are multiple catches possible. My assumption that a single Catch would catch all, seems to be wrong.

Your name is indeed chained to a lot of pages about this subject (looks like there is just one Keith on the entire internet ;-) )
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

kaefer

  • Guest
Re: Does "Catch" catch all exceptions?
« Reply #7 on: February 24, 2012, 02:00:03 PM »
My assumption that a single Catch would catch all, seems to be wrong.

Not at all. You'd better catch System.Exception, the mother of all exceptions.
« Last Edit: February 24, 2012, 02:04:50 PM by kaefer »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Does "Catch" catch all exceptions?
« Reply #8 on: February 24, 2012, 02:02:24 PM »
I don't know about that ;-)

You are using Autodesk.AutoCAD.Runtime.Exception, try System.Exception instead
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Does "Catch" catch all exceptions?
« Reply #9 on: February 24, 2012, 02:37:58 PM »
Ah, my assumption was right but the implementation was not correct :-)

Thanks!
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Does "Catch" catch all exceptions?
« Reply #10 on: February 24, 2012, 04:00:33 PM »
Ah, my assumption was right but the implementation was not correct :-)

Thanks!

Would you care to qualify the assumption and the implementation ?

Regards,
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

TheMaster

  • Guest
Re: Does "Catch" catch all exceptions?
« Reply #11 on: February 25, 2012, 01:34:33 AM »
not if you're trying (anticipating) to catch a
System.Net.WebException

I was not aware of WebExceptions are not catched in a general Exception. In this function I am not really interested in why there is no internet connection but catch any error to be able to continue without an internet connection. Now I still got a runtime error, and because my application implements IExtensionApplication, I did not see the error. The application just didn't run. AutoCAD killed the process silently  :x

If code called from IExtensionApplication.Initialize() throws an exception,
AutoCAD catches it, and aborts the initialization process (which includes
defining commands and other stuff), and you will not know it happened.

As a general rule, you should always wrap the code in your implementation
of IExtensionApplication.Initialize() in a try/catch block, and in the catch block,
be sure to make some noise.


huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Does "Catch" catch all exceptions?
« Reply #12 on: February 25, 2012, 03:56:01 AM »
...

If code called from IExtensionApplication.Initialize() throws an exception,
AutoCAD catches it, and aborts the initialization process (which includes
defining commands and other stuff), and you will not know it happened.

As a general rule, you should always wrap the code in your implementation
of IExtensionApplication.Initialize() in a try/catch block, and in the catch block,
be sure to make some noise.

Indeed. But some things you can learn only from experience, not from books.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

huiz

  • Swamp Rat
  • Posts: 919
  • Certified Prof C3D
Re: Does "Catch" catch all exceptions?
« Reply #13 on: February 25, 2012, 04:02:00 AM »
Ah, my assumption was right but the implementation was not correct :)

Thanks!

Would you care to qualify the assumption and the implementation ?

Regards,

I thought "Catch ex as Exception" would catch all. That was my assumption. My assumption was right, because "Catch ex as Exception" will catch all exceptions. But the implementation was incorrect because I declared ex as AutoCAD.Runtime.Exception. I should have used System.Exception.

Like your subtitle: "Everything will work just as you expect it to, unless your expectations are incorrect." :-)
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

TheMaster

  • Guest
Re: Does "Catch" catch all exceptions?
« Reply #14 on: May 14, 2012, 03:14:28 AM »

I thought "Catch ex as Exception" would catch all. That was my assumption. My assumption was right, because "Catch ex as Exception" will catch all exceptions. But the implementation was incorrect because I declared ex as AutoCAD.Runtime.Exception. I should have used System.Exception.

Like your subtitle: "Everything will work just as you expect it to, unless your expectations are incorrect." :-)

catch( System.Exception ) will not catch all exceptions.

In order to catch an exception and wrap it in an Exception object, the runtime must be able to map the exception to a managed exception type.  This is not always possible. For example, C++ can throw anything, not just exception objects, and .NET cannot wrap anything thrown by C++ code.

To catch almost any exception you need a catch {} without the Exception parameter, but even that will not catch all exceptions. Most notably, StackOverflowException and in .NET 4.0, AccessViolationException cannot be caught, and they will simply terminate the host process.