I'm pretty new to OO and was wondering is it best to handle exceptions within an object and provide feedback to the user or let the exception be handled in code or throw a new exception containing a custom message:
eg In the example below I handle the exception in the object and set a bool to false and provide feedback to the user from the object if an exception occurs.
Is this an appropriate way of using exceptions or should I throw an exception from this object or not do anything and just let the object be caught in code.
Any help / advice / urls on good object design appreciated.
public class SchemaValidator
{
private string xsdSchemaFilePath;
private bool fileIsValid;
private StringBuilder failureLog;
//constructor ommitted
public void validate()
{
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add( this.nameSpace, this.xsdSchemaFilePath );
try
{
//code that may produce exception
}
catch
{
this.failureLog.Append( "The XML document " );
this.failureLog.Append( this.xmlFilePath );
this.failureLog.Append( " is not well formed|" );
this.fileIsValid = false;
}
finally
{
//clean up
}
}
public bool FileIsValid
{
get
{
return this.fileIsValid;
}
}
public string FailureLog
{
get
{
return this.failureLog.ToString();
}
}
}
eg In the example below I handle the exception in the object and set a bool to false and provide feedback to the user from the object if an exception occurs.
Is this an appropriate way of using exceptions or should I throw an exception from this object or not do anything and just let the object be caught in code.
Any help / advice / urls on good object design appreciated.
public class SchemaValidator
{
private string xsdSchemaFilePath;
private bool fileIsValid;
private StringBuilder failureLog;
//constructor ommitted
public void validate()
{
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add( this.nameSpace, this.xsdSchemaFilePath );
try
{
//code that may produce exception
}
catch
{
this.failureLog.Append( "The XML document " );
this.failureLog.Append( this.xmlFilePath );
this.failureLog.Append( " is not well formed|" );
this.fileIsValid = false;
}
finally
{
//clean up
}
}
public bool FileIsValid
{
get
{
return this.fileIsValid;
}
}
public string FailureLog
{
get
{
return this.failureLog.ToString();
}
}
}