User Defined Exceptions


In some situations when you are working with some code we know that code will raise an Exception(But that exception will not harm). Hence we need to bypass that exception and execute the next lines of Code or even sometimes we need to give some Custom (User defined)exception.In which scenario it will be Useful.And how to work it out?I will discuss in this Article with code Snippets

Sometimes when you are working with some fckEditor Control or ext.netfckEditor Control or with some Third party dlls when we fetch the data from the Database and setting the data or values in these controls an exception will raised and at the same time the data/values will set in these editor controls properly.Actually we need to ignore that exception which will not harm for the application/End User and the User should not know that the Exception has raised.


try
{
ddlSender.SelectedValue = Convert.ToString((Information.IsDBNull(objDs.Tables[0].Rows[0]["CMP_INT_SID"]) ? "0" : objDs.Tables[0].Rows[0]["CMP_INT_SID"]));
txtMark.Text = objDs.Tables[0].Rows[0]["CMP_INT_SID"].toString();
fckeditor.Value = dataset.tables[0].rows[0].columns[0].tostring();

}
Catch(Exception ex)
{
if(ex.tostring().Contains("Thread dll Component exception"))
{
// no need to do any thing

}
else
{
lblmessage.Text =ex.Message.Tostring();
}
}


In the Above Code i am setting the data to the controls.once i am executing the line which is in try block it is raising an exception and also sets the data in that control.The Exception which was raised while setting the data in the control will not be harm for application as well as end user.But that will stop the execution of the other lines of code or make an acknowledgment with an error message to the End User(which is not use ful to the end user).So i caught that exception and ignore the exception.

Note : In Visual studio .Net you carefully observe there are innerexceptioncodes are present those Exception codes are Unique.So we can Use that(Exception codes) to ignore for these type of exceptions

Note : If the exception is a different one i will not ignore that i caught that exception and put in a Label i.e. lblExMessage.

Sometimes when you catch an exception that exception will not understand by the End User In this Case the above code is Useful that we call it as a Custom Exception.

for Example see the Below code



Try
{

}
Catch(Exception ex)
{
if(ex.tostring().Contains(Foriegn key Constraint in the tbl_fk_Name Command not Executed properly...."))
{
lblMessage.Text= "Information not Saved/Failed to save Details";
}
}




A simple Message we need to show to the End User i.e. Failed to save Details.So that it will be easily known to the End User.


Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 73 articles authored by srirama

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: