On Error translation

  • Thread starter Thread starter tiger79
  • Start date Start date
T

tiger79

Hello,
I'd like to know what the C# counterpart is for the VB On Error statement ?
My VB code looks like this :

On Error GoTo ErrGetItem

plain simple ud say, but I need to "translate" it into C#...
Any ideas ???
 
Hi tiger79,

I'm not entirely certain how On Error works, but I think you may mean try-catch(-finally)

// code

try
{
// code that may case exceptions
}
catch(SpecificException ex)
{
// error handling code for specificexception
}
catch(GeneralException ex)
{
// error handling code for all generalexceptions not caught by specific...
}
catch(Exception ex)
{
// catch everything still not caught
}
finally
{
// code that needs to run no matter what
}

// code that runs if no exception was thrown
 
hhhmmm... that may work... i'll try it out as soon as I can, cause I got
other problems as well, look at my newer post ;)
thanx anyways...
 
No direct translation is possible for that single line - the VB to C#
converter Instant C# (www.instantcsharp.com) has the
following example:

VB code with On Error ... statements:
Public Sub UnstructuredErrorHandlingExampleOne()

On Error GoTo ErrorHandler

'... <main logic>

On Error Resume Next 'this won't be converted

'... <more main logic>

'turn off error handling:
On Error GoTo 0

'... <more main logic>

Exit Sub

ErrorHandler:
'... <error handling code>
Resume 'this won't be converted

End Sub

C# code produced by Instant C#:
public void UnstructuredErrorHandlingExampleOne()
{
string ActiveErrorHandler = "";
try
{

// On Error GoTo ErrorHandler
ActiveErrorHandler = "ErrorHandler";

//... <main logic>

//INSTANT C# TODO TASK: The 'On Error Resume Next' statement is not
converted by Instant C#:
On Error Resume Next //this won't be converted

//... <more main logic>

//turn off error handling:
// On Error GoTo 0
ActiveErrorHandler = "";

//... <more main logic>

return;

}

catch
{
if (ActiveErrorHandler == "ErrorHandler")
{
//... <error handling code>
//INSTANT C# TODO TASK: The simple 'Resume' statement is not converted
by Instant C#:
Resume //this won't be converted

}
}
}
 
Hi, JetsonG

Replace on error with try
Put catch / end try before end sub or before next on error if you have it

HTH
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top