Continuing execution following an Exception, and Exiting a Function...

R

RSH

Hi,

I have two questions both by a VB programmer that is learning C#...

1) In a VB error trap routine we would commonly trap the error and write it
to an error log and then attempt to "Resume Next." I have set up an
Exception Handling routine in C# and I would like to do the same thing. How
do I call an equivelent of "Resume Next" in C#

2) I have a situation in which I am performing a logical evalutaion
following a database call at the beginning of a function. If the evaluation
returns true then I have no reason to run the rest of the function...so how
do I call the equivelent of VBs "Exit Function...or Exit Sub Routine" ?


Thank you for your help!
Ron
 
M

Mattias Sjögren

1) In a VB error trap routine we would commonly trap the error and write it
to an error log and then attempt to "Resume Next." I have set up an
Exception Handling routine in C# and I would like to do the same thing. How
do I call an equivelent of "Resume Next" in C#

You write try/catch/finally blocks as needed. Execution continues
after the block after any exception handler and finally block has
executed.

2) I have a situation in which I am performing a logical evalutaion
following a database call at the beginning of a function. If the evaluation
returns true then I have no reason to run the rest of the function...so how
do I call the equivelent of VBs "Exit Function...or Exit Sub Routine" ?

return;

or

return yourReturnValue;


Mattias
 
C

Chris Dunaway

Just a couple of general answers to your questions. Others may have
better solutions.

1. I assume you have vb code similar to this:

On Error GoTo ErrorHandler
'Some code here that might cause an error
ErrorHandler:
'Log error here
Resume Next

Generally speaking you would use code similar to this in C# (watch out
for typos):

try
{
'Some code here that might cause an exception
}
catch (Exception ex)
{
'Log exception here
}
'Code execution resumes here

2. To exit a method in C# you simply call return
 

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

Top