By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

try catch with return value

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.