Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resuming Execution from a Catch block

Status
Not open for further replies.

stephenk

Programmer
Mar 12, 2001
21
AU
In VB6, you could handle specific exceptions and then resume at the line of code that caused the problem to 'try again'.

In VB.net, I cannot see how this might be possible in a Try...Catch routine

If I can trap a specific exception that I know how to resolve, how can I return (resume) to the line of code that caused the problem?

For example:

Try
... other code ...

m_WordDoc = oWordApp.Documents.Open(DocPath)

... other code ...

Catch COMexc As COMException
Select Case COMexc.ErrorCode
Case -2146823114 ' doc not found
m_WordDoc = oWordApp.Documents.Add()
m_WordDoc.SaveAs(DocPath)
' happy to continue at this point
Case -2147023174 ' rpc server unavailable
owordapp = new word.application()
Resume????? Try Again?????
Case Else
MessageBox.Show(COMExc.ErrorCode)
End Select
Catch exc As Exception
MessageBox.Shgow(exc.Message)
End Try

The Error Numbered -2147023174 occurs very rarely and does not warrant checking prior to executuion each time.

Am I missing something else. How can I "Resume"...

Thanks
 
Here are two ideas,

1. Check for the potential error before it becomes an error so you do not fall into the error handling.

2. Setup the return to code as it's own function then call the function from the error handling. (I'm not sure how well this would work.)

Kris
 
You can't in a Try Catch block but you can still use the old VB error handlers.

Craig
 
Code:
Dim functionDidntWork as boolean = True
While functionDidntWork
    Try
        FuntionToTry()
        functionDidntWork = False
    Catch ex as Exception
        'Do nothing
    End Try
End While
If FunctionToTry errors out the line which changes functionDidntWork to false won't be called, and thus your while will continue. Might want to add a counter or something to make sure you don't end up with an infinite loop.
 
Code:
Try
   ... other code ...

   Try
     m_WordDoc = oWordApp.Documents.Open(DocPath)
   Catch COMexc as ComException
     if COMexc.ErrorCode = -2146823114 Then
       doc not found
       m_WordDoc = oWordApp.Documents.Add()
       m_WordDoc.SaveAs(DocPath)
       ' happy to continue at this point
     elseif COMexc.ErrorCode = -2147023174 then
       ' rpc server unavailable
       owordapp = new word.application()
       m_WordDoc = oWordApp.Documents.Add()
       m_WordDoc.SaveAs(DocPath)
       ' happy to continue at this point
     else
       throw(COMexc)
     end if
   end try
   ... other code ...
Catch exc As Exception
   MessageBox.Shgow(exc.Message)
End Try

It's all about WHERE you handle your error.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
this thread was started in 2003.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
How strange for it to be answered nearly two and a half years later!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
>How strange for it to be answered nearly two and a half years later!

We were waiting for technology to catch up? [borg2]
 
I hope the poster hasn't been waiting!


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Waiting, no. But I did get my alerts that someone had responded. At least we know the system works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top