473,804 Members | 2,946 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SelectSingl eNode("descenda nt::companyinfo
[@a:sourceid='19 0']/sourcekey/id", nsmgr).InnerTex t

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 4943
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 SelectSingleNod e to make sure it is not null, and only
then proceed with whatever else you were going to do.

"Dave" <an*******@disc ussions.microso ft.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.SelectSingl eNode("descenda nt::companyinfo
[@a:sourceid='19 0']/sourcekey/id", nsmgr).InnerTex t

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.SelectSingl eNode("descenda nt::companyinfo[@a:sourceid='19 0']/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*******@disc ussions.microso ft.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.SelectSingl eNode("descenda nt::companyinfo
[@a:sourceid='19 0']/sourcekey/id", nsmgr).InnerTex t

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
5207
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") '// This record does not exist thus throwing up an error. I could use On
2
6813
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 you are debugging within IDE...? Here is an example from VB: Private SomeFunction () On Error GoTo ErrHandler: MyMethod this, that
5
8613
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 the On Error Resume next Statement, to avoid skipping the process of Inserting the records. But the Problem is
3
27440
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 message from Access. If I comment out the 1st On Error Resume Next and the x = 1 / 0 on the next line there is no difference, i.e. ther same unhandled error on the same line. I included these 2 lines of code to demonstrate that On Error Resume Next...
7
21971
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
3788
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 doesn't move to next element... this sad thing but, it's indication that we must move to try catch instead of on error.
7
6495
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 the next statement when an error can be ignored? Thanks, Rob.
11
48014
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
2027
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 Err1:
0
9571
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.