try catch with return value

D

Doug

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
 
G

Guest

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;
}
}
 
B

Bobbo

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;
}
 
D

DeveloperX

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.
 

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