Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27329

Is it considered 'bad form' to use Try-Catch in a Finally Block?

$
0
0
I am creating a mail merge, and wish to release the object once I am done with it.

Code:

Dim oWord As New Word.Application
        Dim oDoc As Word.Document

        Try

            oDoc = oWord.Documents.Open("C:\Desktop\doc.docx", , True)
            oWord.Visible = True


            oDoc.MailMerge.OpenDataSource("C:\mytestDB.accdb", , , True, True, , , , , , , Connection:="RENEWAL", SQLStatement:="SELECT * FROM [RENEWAL]")
            oDoc.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument

            oDoc.MailMerge.Execute()

            oDoc.Application.Quit(Word.WdSaveOptions.wdDoNotSaveChanges)
        Catch Ex As Exception
            MsgBox("An Error Occured! " & Ex.Message)
        Finally

            Try

                Marshal.ReleaseComObject(oDoc) 'release backwards to creation
                Marshal.ReleaseComObject(oWord)
            Catch ex As Exception
                MsgBox("Could not release object!")
            End Try



        End Try

Simply put, is it bad practice to use TRY-CATCH inside the finally block? As well, my code is complaining because I am releasing oDoc with the possibility of never assinging it. What would be the best way to handle said issue?

Viewing all articles
Browse latest Browse all 27329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>