Results 1 to 5 of 5

Thread: How to handle exception in catch block in c#

  1. #1
    Junior Member
    Join Date
    May 2008
    Answers
    1

    How to handle exception in catch block in c#

    How to handle exception in catch block in c#


  2. #2
    Junior Member
    Join Date
    May 2008
    Answers
    2

    Re: How to handle exception in catch block in c#

    To handle exception in c# we use
    try
    {
    //code
    }
    catch(Exception ex)
    {
    // Message to show exception.
    }


  3. #3
    Expert Member
    Join Date
    Dec 2007
    Answers
    138

    Re: How to handle exception in catch block in c#

    Totally agree for his answer. and the if you have condition/statement that need to execute alyways then can use finally block. also try can have mulitple catch block.


  4. #4
    Junior Member
    Join Date
    Jul 2008
    Answers
    3

    Re: How to handle exception in catch block in c#

    C# provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process. The general form try-catch-finally in C# is shown below

    try
    {
    // Statement which can cause an exception.
    }
    catch(Type x)
    {
    // Statements for handling the exception
    }
    finally
    {
    //Any cleanup code
    }


    Exception Handling in C#

    using System;
    class MyClient
    {
    public static void Main()
    {
    int x = 0;
    int div = 0;
    try
    {
    div = 100/x;
    Console.WriteLine(“This line is not executed”);
    }
    catch(DivideByZeroException de)
    {
    Console.WriteLine("Exception occured");
    }
    Console.WriteLine("Result is {0}",div);
    }
    }

    Last edited by prsoorya; 07-29-2008 at 01:18 AM.

  5. #5
    Junior Member
    Join Date
    Feb 2007
    Answers
    7

    Re: How to handle exception in catch block in c#

    Hi,
    Use this following syntax
    try
    {

    }
    catch(Exception ee)
    {

    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact