www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons C# Exception Handling Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
The exception handling system in C# is very similar to Java. As in Java, the exception handling mechanism of C# allows us to handle errors in a controlled manner. This allows us to separate the normal flow of the code from error handling logic. An exception can represent a variety of abnormal conditions, for example, the use of a null object reference detected by the runtime system or an invalid input string entered by a user and detected by application code. In technical terms, code that detects an error condition throws an exception and code that handles the error catches the exception.

An exception in C# is an object that encapsulates information about the error that just occurred, such as the stack trace at the point of the exception and a descriptive error message. All exception objects are instantiations of the System.Exception class or a sub-class of the System.Exceptoion class. The .NET Framework also provides many built-in exception classes that we can use for various purposes. Sometime we also need to define our own exception classes inheriting from System.Exception or some other appropriate exception class from the .NET Framework.

As in Jave we use try, catch and finally blocks of code to handle exceptions. The following example demonstrates the basics of exception throwing and handling:
class MyExceptionEx
   public static void Main() 
      catch (Exception e) 
      Console.WriteLine("press enter to continue..."); 

   private static void MyMethod1(int value) 
      if (value> 10) 
         throw new Exception( "Invalid value. Out of Range!”); 
      Console.WriteLine("Valid Value”); 
When run, this example produces the following output: Valid Value Invalid value. Out of Range! press enter to continue... We have to enclose the code in a try block that may throw an exception. Following a try block there may be one or more catch blocks. These blocks contain the exception handling logic. When an exception matching the type of the catch block is thrown, that exception object is passed in to the catch block and the control also transfers to the catch block. In the above code when our second call to MyMethod() generates an exception and the catch block then executes. Note that the finally block always executes, even when the exception is not thrown, provided that the execution enter the try block associated with a finally block.
Next >>> Lesson No. 19: C# Generics

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.