473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Can you mimic 'Resume Next' in C#?

Hi,

I have a C# program that is parsing an XML file and
loading a database table. Some of the elements may be
missing but I want to continue with loading the data
anyway because they may not be applicable.

So, if '190' doesn't exist using the following code:

doc.SelectSingleNode("descendant::companyinfo
[@a:sourceid='190']/sourcekey/id", nsmgr).InnerText

will raise:

"Object reference not set to an instance of an object."

I just want to continue processing subsequent nodes. How
can I mimic the resume next in VB. Can you test for
certain exceptions and decide to continue.

Thanks Dave
Nov 15 '05 #1
3 4921
There is no equivalent.

You could put a try/catch around every statement, but that has 2
implications:
1) Your code will be littered with try/catch blocks and hard to read - not
to mention much longer
2) Catching/processing exceptions is a costly thing. If half your nodes are
missing, and you are throwin/catching dozens/hundreds of exceptions, your
code would run noticeably slower.

Your best bet is to code in such a way as to avoid exceptions being raised
in the first place. The way you would prefer to have is just lazy coding -
the most efficient way is to just avoid having these exceptions thrown in
the first place.

Test the result of SelectSingleNode to make sure it is not null, and only
then proceed with whatever else you were going to do.

"Dave" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hi,

I have a C# program that is parsing an XML file and
loading a database table. Some of the elements may be
missing but I want to continue with loading the data
anyway because they may not be applicable.

So, if '190' doesn't exist using the following code:

doc.SelectSingleNode("descendant::companyinfo
[@a:sourceid='190']/sourcekey/id", nsmgr).InnerText

will raise:

"Object reference not set to an instance of an object."

I just want to continue processing subsequent nodes. How
can I mimic the resume next in VB. Can you test for
certain exceptions and decide to continue.

Thanks Dave

Nov 15 '05 #2
> "Object reference not set to an instance of an object."

You just need to break it into two statements:

XmlNode node =
doc.SelectSingleNode("descendant::companyinfo[@a:sourceid='190']/sourcekey/i
d", nsmgr)
string value;

if (node != null)
value = node.InnerText;

HTH,
Eric Cadwell
http://www.origincontrols.com
Nov 15 '05 #3
Behind the scenes, all Resume Next does is wrap every single statement in a
try catch block. If you are a VB programmer, take one of your programs that
used Resume Next and look at the IL. It's awful that VB.NET lets you use it
all, and it should be avoided at all cost.

Not every line needs a wrapper so it makes no sense to use Resume next
nowadays. Instead, wrap the lines that you know are risky and trap specific
exceptions. It's more verbose for sure, but it's infitinetely better as a
methodology.

HTH,

Bill
"Dave" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Hi,

I have a C# program that is parsing an XML file and
loading a database table. Some of the elements may be
missing but I want to continue with loading the data
anyway because they may not be applicable.

So, if '190' doesn't exist using the following code:

doc.SelectSingleNode("descendant::companyinfo
[@a:sourceid='190']/sourcekey/id", nsmgr).InnerText

will raise:

"Object reference not set to an instance of an object."

I just want to continue processing subsequent nodes. How
can I mimic the resume next in VB. Can you test for
certain exceptions and decide to continue.

Thanks Dave

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
2
by: Darta | last post by:
OK- I volunteer to be shot if this question is stupid... BUT, is there a way to, when you catch an exception in the catch{} handler, resume to the same line of code that generated an error while...
5
by: itsupport1 | last post by:
Hi, I am importing some records from one table to another table, and due to Constraints in the destination table, it Throws an Exception and Skip the Whole Rest of records. So I did implement...
3
by: bob.needler | last post by:
I know On Error Resume Next is generally considered lazy. But can someone tell me why the resume next in Exit_Handler does not seem to work? It generates the typical unhandled runtime error...
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
4
by: Neo | last post by:
I found on error resume next doesn't work in for each... e.g. on error resume next for each x in y 'do stuff next if you have an error in for each loop, it falls in infinite loop... it...
7
by: Rob R. Ainscough | last post by:
In VB6 I can use Resume Next to execute the line of coding following the line that cause an exception. There doesn't appear to be anything similiar when using Try...Catch -- so how can one resume...
11
by: Maxwell2006 | last post by:
Hi, I know that this is not a good practice, but I wonder do we have "on error resume next" in C#? Thanks,
11
by: fniles | last post by:
In VB 6 I can do the following: Sub MySub on error goto Err1 : --all my codes are here : --say this is where the error occurs : --this is where resume next will bring me after Err1 exit sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.