You must Sign In to post a response.
  • Category: ASP.NET

    How to skip further execution if exception is catch

    I am using web application.I am using aspx page as api.From function I am calling function 2.Both function have try and catch block.

    Function1()
    {
    try
    {
    int b = function2()
    }
    catch(Exception ex)
    {
    Response.Write(ex.Tostring());
    }

    }

    public int Function2()
    {
    int a= 0;
    try
    {
    a=8;
    return a;
    }
    catch(Exception ex)
    {
    Response.Write(ex.Tostring());
    return a;
    }

    }

    I want to skip further execution(function1) if error catch in second funtion.can I use break in second function's catch block.
  • #768726
    Function1()
    {
    try
    {
    int b = function2()
    }
    catch(Exception ex)
    {
    Response.Write(ex.Tostring());
    }

    }

    public int Function2()
    {
    int a= 0;
    try
    {
    a=8;
    return a;
    }
    finally
    {
    Response.Write(ex.Tostring());

    }
    return a;
    }

  • #768732
    Hi,

    break function won't work, because it skips the current function only not the parent. My suggestion is you already use the return statement in function2, then simply check the condition if the return contains value then continue function1 execution or else just skip the execution.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #768743
    Hai Pinky,
    You can use the return statement if you want to skip the section after the catch block executes.
    You can modify your code as below:

    Function1()
    {
    try
    {
    int b = function2()
    }
    catch(Exception ex)
    {
    Response.Write(ex.Tostring());
    return '-1';
    }
    }

    public int Function2()
    {
    int a= 0;
    try
    {
    a=8;
    return a;
    }
    catch(Exception ex)
    {
    Response.Write(ex.Tostring());
    return -1;
    }
    return 0;
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


Sign In to post your comments