C
C
Croners2015-05-08 11:56:30
Visual Basic
Croners, 2015-05-08 11:56:30

Why is the exception not being caught in the parallel loop?

In this code, dividing by 0 throws an exception that is not caught by the Try block. How to correctly catch exceptions in parallel threads?

Imports System.Threading.Tasks

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        pr()
    End Sub

    Private Sub pr()
        Try
            Dim z As Long
            Parallel.For(0, 2,
                Sub(i)
                    z = 1 / i
                End Sub)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub

End Class

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Croners, 2015-05-18
@Croners

This option is not very suitable, since in this case it is known exactly where the exception can occur in the procedure, and this place can be enclosed in a Try...Catch block. But if the procedure is complicated, then it is impossible to guess where the exception will pop up. Therefore, I would like to intercept, how to say at the input, all possible exceptions ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question