473,379 Members | 1,491 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,379 software developers and data experts.

try catch with return value

Hi

i have a method that returns a value
public bool readxml (string xmlFilename, out string value)
but I would like to catch an exception if it occurs in the method .

How do i catch the following error if the xmlField 'location' doesn't exist
in the xmlfile or if the xmlfile is blank?
public bool readxml(string xmlFilename, out string value)
{
XmlTextReader tr = new XmlTextReader(xmlFilename);
{

if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
return true;
}
else
{
value = null;
tr.Close();
return false;
}
}

Many thanks,

Doug


Jan 31 '07 #1
3 15374
public bool readxml(string xmlFilename, out string value)
{
try{
using( XmlTextReader tr = new XmlTextReader(xmlFilename))
{

if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
return true;
}
else
{
value = null;
tr.Close();
return false;
}
}catch( Exception ){
value = null;
return false;
}
}

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Doug" wrote:
Hi

i have a method that returns a value
public bool readxml (string xmlFilename, out string value)
but I would like to catch an exception if it occurs in the method .

How do i catch the following error if the xmlField 'location' doesn't exist
in the xmlfile or if the xmlfile is blank?
public bool readxml(string xmlFilename, out string value)
{
XmlTextReader tr = new XmlTextReader(xmlFilename);
{

if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
return true;
}
else
{
value = null;
tr.Close();
return false;
}
}

Many thanks,

Doug


Jan 31 '07 #2
On 31 Jan, 08:42, Ciaran O''Donnell
<CiaranODonn...@discussions.microsoft.comwrote:
public bool readxml(string xmlFilename, out string value)
{
try{
using( XmlTextReader tr = new XmlTextReader(xmlFilename))
{

if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
return true;
}
else
{
value = null;
tr.Close();
return false;
}}catch( Exception ){

value = null;
return false;
To expand on Ciaran's suggestion a little, I believe it's considered
good practice to catch specific exceptions (i.e. ones you can do
something about, such as notifying if a file related to your app isn't
found or an XML parse error occurs) and let general ones propagate up
the call stack. Note that you can catch multiple specific exceptions.

There is a FileNotFoundException in System.IO but you'll need to add a
'using' reference to it to use it. XmlException will fire if there is
a parse error in your document.

public bool readxml(string xmlFilename, out string value)
{
bool ret = false;
try
{
using (XmlTextReader tr = new
XmlTextReader(xmlFilename))
{
if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
ret = true;
}
else
{
value = null;
tr.Close();
ret = false;
}
}
}
catch (XmlException)
{
// any logging code goes here
value = null;
}
catch (FileNotFoundException)
{
// any logging code goes here
value = null;
}
return ret;
}

Jan 31 '07 #3
It's also worth looking at
AppDomain.CurrentDomain.UnhandledException.
This is used when an exception occurs that isn't handled, so you will
still have an opportunity to at least catch it, log it, warn the user
in a less brutal way that just having the app disappear.

One of the reasons to use specific exceptions is that it shows you're
seperating expected errors that could occur, like files being locked
from unanticipated errors caused by coding errors. If you catch
Exception expecting that to indicate a file is locked and some code
throws a different exception you may take the wrong action.

Finally you should order the catch statements from the most derived to
the least derived, ie the most specific to the least specific. So you
might try and catch RowNotInTableException first, then DataException
next. If you order them the other way round you will always end up in
the DataException block and never in the RowNotInTableException
block.
On 31 Jan, 09:44, "Bobbo" <robin.willi...@choicequote.co.ukwrote:
On 31 Jan, 08:42, Ciaran O''Donnell

<CiaranODonn...@discussions.microsoft.comwrote:
public bool readxml(string xmlFilename, out string value)
{
try{
using( XmlTextReader tr = new XmlTextReader(xmlFilename))
{
if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
return true;
}
else
{
value = null;
tr.Close();
return false;
}}catch( Exception ){
value = null;
return false;

To expand on Ciaran's suggestion a little, I believe it's considered
good practice to catch specific exceptions (i.e. ones you can do
something about, such as notifying if a file related to your app isn't
found or an XML parse error occurs) and let general ones propagate up
the call stack. Note that you can catch multiple specific exceptions.

There is a FileNotFoundException in System.IO but you'll need to add a
'using' reference to it to use it. XmlException will fire if there is
a parse error in your document.

public bool readxml(string xmlFilename, out string value)
{
bool ret = false;
try
{
using (XmlTextReader tr = new
XmlTextReader(xmlFilename))
{
if (tr.ReadToFollowing("location"))
{
value = tr.ReadString();
tr.Close();
ret = true;
}
else
{
value = null;
tr.Close();
ret = false;
}
}
}
catch (XmlException)
{
// any logging code goes here
value = null;
}
catch (FileNotFoundException)
{
// any logging code goes here
value = null;
}
return ret;
}- Hide quoted text -

- Show quoted text -

Jan 31 '07 #4

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

Similar topics

18
by: deanbrown3d | last post by:
I mean, is this correct? try { Screen->Cursor = crHourglass; Do something bad return false; else return true; }
6
by: George | last post by:
Hi All, I thought this would not compile because no return value is specified. But it does compile and run (aix and xlc v7.0.) Could someone kindly please point me to where in the spec this...
17
by: hplloyd | last post by:
Hi, I have a function that adds data to a database and I want to return true if the database was updated and false if there was a problem, so I am using a try... catch block... My problem is...
3
by: Neelesh Bodas | last post by:
Hello all, Just wanted a small clarification on these two points : 1) The following code compiles well - int f() try { throw "xyz"; } catch(...) {
2
by: Eric Lilja | last post by:
Hello, consider this complete program: #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class Hanna {
7
by: Rudy | last post by:
Hello All! I have a value in a textbox(txbTableIDm.Text ) that I would like to use in a paremiter in a SP I wrote, and then have the select statement work off that parememter, retireive a...
1
by: JIM.H. | last post by:
Hello, I am trying to learn web service implementation, I have a question about return value. I will be returning and xml string with many records if everything is fine and the reader reads it and...
3
by: srinath duraisamy | last post by:
Hi Is there any way to catch the return value of the main program? Regards Srinath.D
4
by: Polaris431 | last post by:
If I have a method like this: private void DoSomething() { try { return true; } catch{} finally
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.