Exception handling in .Net framework - DotNet and DataBase

Tuesday, 3 July 2018

Exception handling in .Net framework





While running a program, knowingly or unknowingly  there is a chance to happen some run time errors.

These run time errors stops/hangs the execution of the program and finally and confuses the end user.

We can find errors in a program in different stages, we can classify them as below,

1)Compile time errors
2)Logical errors
3)Run time  errors

Compile time errors
---------------------------
These errors will occur at compilation of a program. Generally these errors says syntax or rule errors.
So, we can call them "Syntax errors".

Logical errors
-------------------
These errors are identified when required output and generated output doesn't get match.

Run time  errors
----------------------
These errors will occur when a program is in execution, due to which program in between gets terminated. (abnormal termination).
To overcome this rum time errors for the smooth execution of program , .Net framework has a concept called exception handling. using "try catch" blocks

Using try catch blocks we can skip run time  errors and alert the programmer to resolve the issues.

Syntax:
-----------
          try
            {
            }
          catch (Exception)
            {
                throw;
            }
          finally
            {
     
            }

In this the actual code resides under try block and any run time error occurs execution skips to catch (Exception) block.
There we can put some alert messages or auto mails with complete error details to alert programmers.
finally block contains some common functionalities which has to execute even the program success or fails.
Let us suppose we are assigning an integer information (which is actually in string format) to  another integer as below.

          string a = "10";
          int b = Convert.ToInt32(a);
          Console.WriteLine("Employee id is : " + b);
          Console.Read();

There is an any problem with above code as type conversion accepts the string a to convert into integer.
But in case the above string contains some unwanted alphabets or special characters as below,

           string a = "10";
            int b = Convert.ToInt32(a);
            Console.WriteLine("Employee id is : " + b);
            Console.Read();

.Net compilers doesn't allow to convert into integer format directly and throws some run time errors as below
"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format."
To overcome this problem we keep the actual code into try, catch block.
So, incase the code fails to execute, compiler shifts to catch block and executes the code on it.

            try
            {
                string a = "10a";
                int b = Convert.ToInt32(a);
                Console.WriteLine("Employee id is : " + b);
             }

            catch (Exception ex)
            {
                Console.WriteLine("there is an issue with the employee id you supplied    " + ex.Message);
            }
                Console.Read();

Here in the above, it is not possible to convert "10a" into integer, so skips the code to catch block and
executes the code on it and we will get the result as below.
"there is an issue with the employee id you supplied    Input string was not in a correct format."
Here  "input string was not in a correct format." is the run time error
Exception  is the class which holds the actual error information.
finally block is to execute the code which is independent of  program performance.
try
            {
                string a = "10a";
                int b = Convert.ToInt32(a);
                Console.WriteLine("Employee id is : " + b);
            }
            catch (Exception ex)
            {
                Console.WriteLine("there is an issue with the employee id you supplied    " + ex.Message);
            }
            finally
            {
                Console.WriteLine("All the best");
                Console.Read();
            }

Result:
---------
"there is an issue with the employee id you supplied    Input string was not in a correct format.
All the best"

Practice

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.