To handle errors in C#, we generally use try, catch and finally methods. but when error occurs in finally block basically three things happens:
.
The exception rises and it needs to
be handled at upper level, but while handling the error our running application may get crashed.
The finally block execution gets
stopped at the point of error. If
there was an error in try block and
then again we face an error in
finally block then the error of the
try block gets lost.
To handle such situations , what we
can do is , we can keep nested try
blocks or put a finally block
within a try block.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebServiceApp
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
myFunction();
}
catch
{
// catch the exception and log into database
}
}
protected void myFunction()
{
try {
// Do something
}
catch {
// catch the exception and log into database
}
finally {
// do something
}
}
}
}
0 Comment(s)