Results 1 to 4 of 4

Thread: Exception Handling in Csharp

  1. #1
    Join Date
    Nov 2009
    Posts
    68

    Exception Handling in Csharp

    I want to know how are Exceptions Handled in C#. I am good at programming and although I have studied Java, I find the Exception Handling part of it very difficult. I am now working on C# and have to make a presentation on Exception Handling in C#. I need help to understand C# Exception Handling in detail.
    Last edited by Jesus2; 11-11-2009 at 10:57 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Exception Handling in Csharp

    An Exception is an unwanted or an unexpected error or bug that may arise in your application or program. With Exception Handling in Csharp you can take care of these Exceptions. As with Java, Csharp Exception Handling also make use of the basic keywords: try, catch and finally. Exceptions are usually arise because of any third-party library or even by the Commom-Language-Runtime. The Exception can also be thrown forcibly by the keyword throw .

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Exception Handling in Csharp

    Java handles the exception using try, catch and finally block. C# also makes use of this same try, catch and finally block for the purpose of Exceptions Handling.
    The format of a try-catch-finally block to handle exception is:
    Code:
    try
     {
      .
      .       // Statement that generate exception/s.
      .
     }
    
    catch(Type a)
     {
      .
      .     // Statements that will handle the generated exception/s
      .
     }
    
    finally
     {
      . 
      .  //This code will always execute
      . 
     }
    You are allowed to use multiple try-catch blocks, but only one finally block is valid.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Exception Handling in Csharp

    If any code is expected to create an exception it is placed in a try block in Csharp. The Catch block contains the code that will take care of the exceptions.

    Here is a very simple example of Exception Handling in C# for an exception caused by dividing a number by zero.

    Code:
    void DivExcep(int a, int b)
    {
        try
        {
          int c;
          c = a/b;
          System.Console.WriteLine(c);
        }
    
        catch (System.DivideByZeroException zer)
        {
            System.Console.WriteLine("Division by zero Not possible");
        }
    }

Similar Threads

  1. Exception Handling in .Net
    By Bruno007 in forum Software Development
    Replies: 5
    Last Post: 25-12-2010, 01:30 AM
  2. Exception Handling in Singleton
    By Bottlenecked in forum Software Development
    Replies: 6
    Last Post: 20-09-2010, 09:14 PM
  3. Exception handling in PL/SQL
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 11:04 PM
  4. Exception handling in EJB
    By KennedII in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 04:40 AM
  5. Problem in handling exception with Regex
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 01:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,492,491.93782 seconds with 16 queries