Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to handle an error occurred in finally block

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 173
    Comment on it

    Hello all,

    To handle errors in C#, we generally use try, catch and finally methods. but when error occurs in finally block basically three things happens: .

    1. The exception rises and it needs to be handled at upper level, but while handling the error our running application may get crashed.
    2. 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.
    3. 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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: