...
VB6 Error Handling uses a number of statements that are not supported in C#:
- On Error Goto <label>: starts a protected block. Errors send execution to a labeled handler block . (aka. OEGT)
- On Error Goto 0: turns off protection
- On Error Resume Next: starts a protected block that ignores all errors until the end of the method or another On Error statement. (aka. OERN)
- Resume
...
- : returns execution to the statement following the one that raised an error
Default Error Handling Upgrades
- OEGT is On Error Goto <label>is migrated to a try/catch. The protected block is placed in the try, and the handler block is placed in the catch.
- On Error Resume Next is migrated to gmRTL.Core.ErrorHandling.ResumeNext ResumeNext. The protected block is a lambda expression passed to this function. The ResumeNext function rewrites the IL at runtime adding try/catch to its statements.
The tool will may suppress these upgrade transformations if it determines doing so will break the build due. For example if the resulting code would have a return from a lambda expression.
...
, the OERN is not applied if the protected code has a return in a lambda expression. When the Error Handling Upgrade cannot be applied automatically, the tool will add UPGRADE_TODO comments to the translation. Note that VB.NET translations retain On Error statements by default. VB.NET translations may be made to use try/catch by adding a <Select UseTryCatch="on"/> command to the translation script.
Suppressing the default Error Handling Upgrade
One known case where the Error Handling migration may break the build is when the .NET logic uses a variable before it is initialized. This can occur when the variable is used in the error handler or a goto block but the initialization is conditional. In this case you may see something like this build error:
...
Code Block |
---|
<Compile> <Refactor> <Migrate id="VBPName.MyFile.Foo" RemoveOnErrorGoto="on"/> </Refactor> </Compile> |
Error Handling Options
The ExceptionHandling Enumerated Attribute
The <Select ExceptionHandling command specifies how the analyzer should deal with exception handling. It possible values are the ExceptionType enumeration whose entries are as follows:
Entry | Description |
Simple | Indicates that logic to set the VBNET.Error object will not be authored. (default) |
SetErrorObject | Indicates that logic should be authored in try/catch blocks so that the VBNET.Err object will be set to reflect the error number. |
UseTryCatch | Indicates that the tool should use Try-Catch as opposed to On Error GoTo style error handling for VBN translations. In essence, this switch tells the analyzer to use the same analyzer method that is presently used for CSH for VBN as well. |
When the SetErrorObject exception type is active the first statement of each catch block has a call to gmRTL.Core.GlobalException.SetErrorObject included. Thus, for example, a simple block becomes
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
catch(Exception exc)
{
gmRTL.Core.GlobalException.SetErrorObject(exc);
}
|