What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4410 |  Welcome, Guest!   Register  Login
Home > Articles > C# > Handling Exceptions in C# Programs

Handling Exceptions in C# Programs

1 vote(s)
Rating: 5 out of 5
Article posted by Naimishforu on 6/26/2011 | Views: 4224 | Category: C# | Level: Intermediate | Points: 250 red flag


In this article I shall demonstrate the exception-handling capabilities of C#.


Introduction

 
After finishing the Part 1, let's create a program that will demonstrate the exception-handling capabilities of C#.   
This program will define an array that contains only one element. 
 
The program will then prompt the user to enter a 0 or a 1. If 0 is entered, program execution performs as expected and the element at an index of zero in the array will be displayed on screen. If 1 is entered, an exception will be thrown, as there is no corresponding index in the array.
 
Step 1 of 4
 
The Main method of this program may throw an exception.
 
Enter the keyword that marks the code the program will attempt to execute.
 
using System; 
  
namespace Exceptions 
{ 
  class ExceptionHandlingTest 
  { 
    static void Main(string[] args) 
    { 
       int[] intArray = {100}; 
       MISSING CODE (You will have to enter TRY Block over here)
    } 
  } 
}

 
Result
 
You type try, which encloses the code in a method that is to be executed but may throw an exception.
 
Step 2 of 4
 
The try block contains code that will be executed and may throw an exception.
 
What will the program do if an exception is thrown?    

using System; 
  
namespace Exceptions 
{ 
  class ExceptionHandlingTest 
  { 
    static void Main(string[] args) 
    { 
      int[] intArray = {100}; 
      try 
      { 
  
       MISSING CODE (You will have to enter proper Catch Block)
      } 
    } 
  } 
}

 
Result
 
If an exception is thrown, the program will unwind the assembly until it finds a catch statement that contains an appropriate exception handler.
 
Step 3 of 4
 
To signal an abnormal condition on C#, you throw an exception.
 
Enter the line of code that creates an instance of System.Exception.
 

using System; 
  
namespace Exceptions 
{ 
  class ExceptionHandlingTest 
  { 
    static void Main(string[] args) 
    { 
      int[] intArray = {100}; 
      try 
      { 
                 Console.WriteLine("Enter 0 for an Exception "); 
                 Console.WriteLine("Enter 1 for IndexOutOfRangeException"); 
                 int index = Int32.Parse(Console.ReadLine()); 
  
                 Console.WriteLine("Number is {0}", intArray[index].ToString()); 
                 
        MISSING CODE; (You can choose any line of code from the Results below)
      } 
    } 
  } 
}

Result
 
In fact, you can use any of the following lines of code to create an instance of System.Exception:
 
throw new Exception() 
throw new System.Exception() 
throw new Exception( ) 
throw new System.Exception( ) 
throw new Exception() 
throw new System. Exception() 
 
Step 4 of 4
 
The array described in this program contains only one element.
 
Which exception will be thrown if the user chooses an element that isn't part of the array?
 

catch (MISSING CODE)

 
Result
 
If the user tries to choose an element that isn't part of the array, the IndexOutOfRangeException exception is thrown.
 
The following is the output of this program when the user enters 0.
 
Enter 0 for an Exception 
  
Enter 1 for IndexOutOfRangeException 
  
0 
  
Number is 100 
  
Exception of type System.Exception was thrown. 
  
There is only one element in this array: 100 
  
And this is the output if the user enters 1. 
  
Enter 0 for an Exception 
  
Enter 1 for IndexOutOfRangeException 
  
1 
  
IndexOutOfRangeException caught, 
  
an index of 1 does not exist in the array! 
  
There is only one element in this array: 100 

 
Thanks for reading.
 

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:5 year(s)
Home page:http://naimishpandya.blogspot.in/
Member since:Saturday, January 22, 2011
Level:Bronze
Status: [Member] [MVP]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

There are 2 possible approaches to use the html to pdf converter in a non .NET application, assuming you have a Windows server which are discussed in this article.

Parallel programming is introduced in C# 4.0 using the Task libraries. There are many aspects of Parallel programming and how to use the Task libraries. Here, we will look into the Task from a beginner stand point. In this article, we will discuss about how to create an asynchronous method using Task.

We all are aware of Operator overloading concept in C#. We can also overload ‘Cast’ operator but a little bit of workaround is required here.

Generally in many websites after you get logged in you will be redirected to home page. While this page is running if the user selects the new window and open the Login page again and if he provides the same credentials which was provided earlier then he will be automatically redirected to home page. But in this article I am going to explain how to restrict this if you consider the examples of gmail.com or yahoo.com once in a particular user is logged in they won’t restrict the user but here I will provide that feature and check whether if a particular user is logged in I will display it as already logged in.

We will see how to work with Network I/O using C#.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 8:55:45 PM