Skip to main content

Exception Handling Mechanism In .NET

By reading the article, you will learn the points given below.
  • What are the types of errors occurring in .NET?
  • Methods to handle exception
  • Program to handle an exception, using Logical Implementation
  • Program to handle an exception, using Try Catch Implementation
  • Top 10 Interview questions on Exception Handling Mechanism
Let’s begin with an introduction on exception handling mechanisms,

When we write some code and execute in .NET, there is a high possibility of an error occurrence, as mentioned below.
  1. Syntactical Error
  2. Compilation Error
  3. Runtime Error
Syntactical Error
This error will occur by typing the wrong syntax like missing double quotes, terminators etc.
We as a programmer can identify these minor errors and can easily rectify.
Compilation Error
This error occurs when the program is compiled and the errors are like assigning wrong data to a variable, creating an object for interface etc.
This error can be identified and rectified before the execution of the program.
Run Time Error
This error will occur at the time of executing the program, errors are like entering the wrong data in to a variable trying to open a file for which there are no permissions, which are trying to connect with the database with wrong credentials etc.
What is Exception?
A run time error is known as an exception, as it will cause abnormal termination of the program execution. To avoid abnormal termination of the program execution, we need to handle the exceptions.
Methods to handle an exception:
There are two methods to handle the exception, which are given below.
  1. Logical Implementation
  2. Try & Catch implementation
Logical Implementation
In this method, we handle the exception by using the logical statements. In real time environments, the first and foremost importance should be given for the logical implementation only.
If it's not possible to handle any exception, using logical implementation, we use try catch implementation.
Let’s see a sample program on how to handle an exception, using Logical Implementation
Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace HandleExceptionDemo  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int a, b, c;  
  14.             Console.Write("Enter any two numbers:");  
  15.             a = Convert.ToInt32(Console.ReadLine());  
  16.             b = Convert.ToInt32(Console.ReadLine());  
  17.             if (b == 0)  
  18.             {  
  19.   
  20.                 Console.WriteLine("Second Number cannot be Zero");  
  21.             }  
  22.             else  
  23.             {  
  24.                 c = a / b;  
  25.                 Console.WriteLine("Result is :" + c);  
  26.             }  
  27.             Console.Read();  
  28.         }  
  29.     }  
  30. }  


When the user enters the second number as zero, an exception will be raised. This is handling, using logical implementation.
Try Catch Implementation
A try block can be followed by any number of catch blocks, where writing finally block is optional.
If catch block is used without an exception class, it is know as Generic Catch block.
If catch block is used with an exception class, it is know as Specific Catch block.
Try
This block contains all the statements in which there is a possibility of an exception occurrence.
Catch
This block contains all the statements to handle the exception, which is raised within the try block.
Process of execution
Execution starts from try block, if there is any exception which occurs in any statement of try block, then the lines of exception statement given below are ignored and control jumps to catch block. Subsequently, catch block is executed and then finally block. It's simple.
Let’s see a sample program to handle an exception, using Try-Catch implementation.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace HandleExceptionDemo  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             int a, b, c;  
  14.             Console.Write("Enter any two numbers:");  
  15.             try  
  16.             {  
  17.                 a = Convert.ToInt32(Console.ReadLine());  
  18.                 b = Convert.ToInt32(Console.ReadLine());  
  19.                 c = a / b;  
  20.                 Console.WriteLine("result is :" + c);  
  21.             }  
  22.             catch  
  23.             {  
  24.                 Console.WriteLine("error Occured");  
  25.             }  
  26.             finally {  
  27.                 Console.WriteLine("Code executed");  
  28.             }  
  29.             Console.Read();  
  30.              
  31.         }  
  32.     }  


Properties of Exception Class
  1. Message
  2. Source
  3. Helplink
A list of exceptions that occur in real-time scenarios are given below.
  1. Argument Exception
  2. ArgumentNullException
  3. ArithmeticException
  4. IndexOutOfRange
  5. NullReferenceException
  6. InSufficientException
  7. T.C.,
Interview Questions on Exception Handling Mechanism
  • What are Runtime Errors?
  • What are the different types of errors which occur during program development and execution?
  • Explain about Logical Implementation
  • Explain about try catch implementation & nested try catch Implementation
  • What are the properties with the Exception Classes?
  • What is an Exception?
  • What is the process of an Exception?
I hope this article helps you to gain knowledge on an exception handling mechanism in .NET.
Thanks for reading and let me know your valuable comments, if any.

Comments

Popular posts from this blog

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation

ERROR: Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_CI_AS_KS_WS in the equal to operation
dont get panic if u get the following error while in your joinings, there is a simple way to solve this.
it is because of collation settings on two columns we are doing joinings. it is because of collation settings on two columns we are doing joinings
First step is to figure out what are two colaltion that cause for conflicts. Let us assume that collation has not been set at the column level and instead at db level, then we have to execute two simple statements as below.

Statements: Select DATABASEPROPERTYYEX('DB1',N'Collation') Select DATABASEPROPERTYYEX('DB2',N'Collation')
One more thing, make a note that if you are on sharepoint you will get error as below
Latin_General_CI_AS_KS_WS. if you are on most any other database and use the dafault settings you may get this SQL_Latin_General_CP1_CI_AS.
Now we have to do some…

How To Create The Data Adapter Object Using ADO.NET by #RathrolaPremKumar

By reading this article, you will learn key points in ADO.NET, as given below. What is Data Adapter?Primary Data Adapters for the databases.Data Adapter properties.Methods used by Data Adapter.How to create Data Adapter with an example? Let’s start our demo with a Data Adapter introduction. Actually, we use Data Adapter object to establish the connection to the data source and manage the movement of date to and from the database. What is Data Adapter? A data adapter aobject serves as a bridge between a data set object and Data Source such as a database to retrieve and save the data. Data adapter contains a set of database commands and a database connection, which we use to fill a dataset object and update the Data Source. Primary data adapters for databases? .NET makes two primary data adapters available for use with the databases. Other data adapters can also be integrated with Visual Studio .NET. Primary Data Adapters are mentioned below. OleDbData Adapter, which is suitable for use wi…

'#Resolve' To export a list, you must have a Microsoft SharePoint Foundation-compatible application.

Searching this error message easily finds the root of the problem. There is a problem with the Office 64 bit version. It doesn’t work with SharePoint. These solutions generally tell you to uninstall your 64 bit office and install a 32 bit version. Happily there is a much easier solution.
Despite the error message the export actually works! Notice the yellow bar at the bottom of your browser. Simply save the .iqy file normally and open it with Excel. It’s that simple. The error message is certainly a nuisance but does not actually stop the export.
Thanks for ready my blog :)