Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Handling Exceptions In Threads

Handling Exceptions In Threads

Scheduled Pinned Locked Moved Visual Basic
designjsonhelpquestionlounge
6 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    I have an application in which i create a thread to run a certain process. There is a api function call in the thread that tries to connect through TCP/IP to a device and get some data from it.If the device is disconnected then I get a socket exception. I need to handle this exception however I found out during debugging that even though the exception was thrown,it went unhandled even though I put a try catch block. However when i run it on the Main UI thread everything is fine. After doing some searches on this issue I have understood that general exceptions cannot be handled in threads?Is this correct? If so,is there a way to handle this socket exception?If any one has any idea about this or can direct me to any good articles on this...really appreciate it..

    L G 2 Replies Last reply
    0
    • S sohaib_a

      I have an application in which i create a thread to run a certain process. There is a api function call in the thread that tries to connect through TCP/IP to a device and get some data from it.If the device is disconnected then I get a socket exception. I need to handle this exception however I found out during debugging that even though the exception was thrown,it went unhandled even though I put a try catch block. However when i run it on the Main UI thread everything is fine. After doing some searches on this issue I have understood that general exceptions cannot be handled in threads?Is this correct? If so,is there a way to handle this socket exception?If any one has any idea about this or can direct me to any good articles on this...really appreciate it..

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I would guess you tried and did something to the GUI inside the catch block, violating the "only main/GUI thread should ever touch any Control" rule, which causes another exception and that one does no longer get caught by your try-catch since you are already in the catch block. Experiment with Console.WriteLine() or some file operation to confirm my theory, then start using the Control.InvokeRequired/Control.Invoke pattern. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      S 1 Reply Last reply
      0
      • S sohaib_a

        I have an application in which i create a thread to run a certain process. There is a api function call in the thread that tries to connect through TCP/IP to a device and get some data from it.If the device is disconnected then I get a socket exception. I need to handle this exception however I found out during debugging that even though the exception was thrown,it went unhandled even though I put a try catch block. However when i run it on the Main UI thread everything is fine. After doing some searches on this issue I have understood that general exceptions cannot be handled in threads?Is this correct? If so,is there a way to handle this socket exception?If any one has any idea about this or can direct me to any good articles on this...really appreciate it..

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        Did you put the exception handler inside the method running on the thread or in the method that started the thread? In general, if an exception occurs on a background thread and that thread does not handle the exception, the thread dies and no notification is made to the main program. Option A (do this)

        Private Sub ThreadRunMethod()
        Try
        'your code here that throws an exception
        Catch ex as Exception
        'something that handles the exception, but
        'does not modify the UI directly.
        End Try
        End Sub

        Private Sub OtherMethod()
        Dim myThread as New Thread(AddressOf ThreadRunMethod)
        myThread.Start()
        End Sub

        Option B (don't do this, the start method will return right away)

        Private Sub ThreadRunMethod()
        'your code here that throws an exception
        End Sub

        Private Sub OtherMethod()
        Dim myThread as New Thread(AddressOf ThreadRunMethod)
        Try
        myThread.Start()
        Catch ex As Exception
        'handle exception from thread method
        End Try
        End Sub

        S 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, I would guess you tried and did something to the GUI inside the catch block, violating the "only main/GUI thread should ever touch any Control" rule, which causes another exception and that one does no longer get caught by your try-catch since you are already in the catch block. Experiment with Console.WriteLine() or some file operation to confirm my theory, then start using the Control.InvokeRequired/Control.Invoke pattern. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          S Offline
          S Offline
          sohaib_a
          wrote on last edited by
          #4

          I didnt understand exactly what you mean.. This is how I am doing it the threading way. Public Sub somfunction() '''I make all the thread declarations (address of startthread) Thread.start() End Sub Public Sub startthread Try ''''APi function call to connect to device --this throws a socket exception when the device is not available but i cannot catch this exception.It throws the exception but it doesn't go to the catch block. catch ex as exception End try End sub But when I do it on the main thread (on the form) Public sub somfunction Try ''API function call----this socket exception is caught Catch ex as exception End try End Sub End Sub

          L 1 Reply Last reply
          0
          • S sohaib_a

            I didnt understand exactly what you mean.. This is how I am doing it the threading way. Public Sub somfunction() '''I make all the thread declarations (address of startthread) Thread.start() End Sub Public Sub startthread Try ''''APi function call to connect to device --this throws a socket exception when the device is not available but i cannot catch this exception.It throws the exception but it doesn't go to the catch block. catch ex as exception End try End sub But when I do it on the main thread (on the form) Public sub somfunction Try ''API function call----this socket exception is caught Catch ex as exception End try End Sub End Sub

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            The idea of a try-catch construct is you put some code in the try block AND some code in the catch block; an empty catch block is a deadly sin, it is worse than no try-catch at all: it swallows the exception and doesn't do anything with it. At least log it somewhere, say Console.WriteLine(ex.ToString()) so you will notice if and when it occurs. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            1 Reply Last reply
            0
            • G Gideon Engelberth

              Did you put the exception handler inside the method running on the thread or in the method that started the thread? In general, if an exception occurs on a background thread and that thread does not handle the exception, the thread dies and no notification is made to the main program. Option A (do this)

              Private Sub ThreadRunMethod()
              Try
              'your code here that throws an exception
              Catch ex as Exception
              'something that handles the exception, but
              'does not modify the UI directly.
              End Try
              End Sub

              Private Sub OtherMethod()
              Dim myThread as New Thread(AddressOf ThreadRunMethod)
              myThread.Start()
              End Sub

              Option B (don't do this, the start method will return right away)

              Private Sub ThreadRunMethod()
              'your code here that throws an exception
              End Sub

              Private Sub OtherMethod()
              Dim myThread as New Thread(AddressOf ThreadRunMethod)
              Try
              myThread.Start()
              Catch ex As Exception
              'handle exception from thread method
              End Try
              End Sub

              S Offline
              S Offline
              sohaib_a
              wrote on last edited by
              #6

              Thanks guys I will try your way and see what happens. Sorry Luc but I had forgot to mention in the example,I have put a error log entry that writes to a text file in the catch block,but the thing is when the exception is thrown it never goes to the catch block.The thread then just dies.I never know that the socket exception occurred. I can only seem to catch the Thread Abort Exception in the thread.I am not sure but I had read this article somewhere on the net that only Thread Abort Exception can be handled in background threads.Thought you guys could give me more info on this.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups