Translate

Thursday, 25 May 2017

Exception Handling in C#

Exception Handling in C#
Q 1) you all have doubt that what is exception and what is exception handling? Before explaining that please have a look at below code:
Code1:
using System;
namespace main_class{
    class MainClass{
        static void Main(string[] args){
        Console.WriteLine("hi this is main function start");
        int a=10;
        int b=0;
        
        int div_result;
        div_result=a/b;
        
        Console.WriteLine("{0}",div_result);
        
        Console.WriteLine("hi this is main function end");
    }
    }
}

Output of code is:
hi this is main function start
Unhandled Exception:
System.DivideByZeroException: Division by zero
  at main_class.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Division by zero
  at main_class.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0


Explanation:
.You are trying to divide number a by 0, which leads to infinity result. 
.Which is undefined number.
.So program execution will stop at that point only. 
.Means it will not execute further instructions of the program.
.Thats why we are not getting the output of the last two console statements.

Code 2:

using System;
namespace main_class{
    class First{
        public void fun(){
            Console.WriteLine("hi this is the fun() method of First class");
        }
    }
    class MainClass{
        static void Main(string[] args){
        Console.WriteLine("hi this is main function start");
        First obj = new First();
        obj=null;
        obj.fun();
        Console.WriteLine("hi this is main function end");
    }
    }  
}
Output of above code is:
hi this is main function start
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at main_class.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at main_class.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0

Explanation:
.In this code you make object of class First as null
.Then you are calling fun() method using that object, which is null
.so here that object reference is not set to instance of an object
.So we will get NullReferenceException
.Once we will get line obj.fun() our program will stop there only, it will not execute further instructions.

Conclusion of above two codes:
So both the errors are coming at runtime(during the execution of program) so these are known as exceptions. 
And Techniques used to handle such kind of exceptions are known as Exception Handling.

Q 2) What is Exception?
Ans ) Let say your program is running and then suddenly you get an error, so this error is known as Exception.
In Terms of Definition : Problem arises during execution of a program is known as Exception.

Q 3) What is Exception Handling?
Ans) Process of handling exception is known as exception handling.

Q 4) How to use exception handling?
Ans:  C# exception handling is build upon  four keywords: try, catch, finally, throw
Let me explain everything with the help of code, I think it will be easy for all of you to understand:

Code:
using System;
namespace main_class{
    class MainClass{
        static void Main(string[] args){
        Console.WriteLine("hi this is main function start");
        int a=10;
        int b=0;
        try{
        int div_result;
        div_result=a/b;
        Console.WriteLine("{0}",div_result);
        }
        catch(DivideByZeroException e){
            Console.WriteLine("you are dividing by zero please re enter the number b");
        }
        
        catch(NullReferenceException e){
            Console.WriteLine("ypu are using a null reference object");
        }
        finally{
            Console.WriteLine("It will always execute whether exception will occur or not");
        }
        Console.WriteLine("hi this is main function end");
    }
    }
    
}


Output:
hi this is main function start
you are dividing by zero please re enter the number b
hi this is main function end


Explanation:
1. Try block: it includes the portion of code where there is a possibility of getting exception.
2. Catch block: 
. We can write multiple catch block after try
. out of those catch block based on the type of exception it will select which catch will handle exception.
3. After executing catch block it will start executing further instruction means our program execution will not stop.
4. finally{} block will always execute whether exception will generate or not.



Throw Block Code:
using System;

namespace main_class{
    class MainClass{
        static void Main(string[] args){
        Console.WriteLine("hi this is main function start");
        int a=10;
        int b=0;
        
        try{
        int div_result;
        if(b==0){
            throw (new DivideByZeroException("you are dividing by zero please re enter the number b"));
        }
        //div_result=a/b;
        //Console.WriteLine("{0}",div_result);
        }
        catch(DivideByZeroException e){
            //Console.WriteLine("you are dividing by zero please re enter the number b");
        }
        
        catch(NullReferenceException e){
            Console.WriteLine("ypu are using a null reference object");
        }
        finally{
            Console.WriteLine("It will always execute whether exception will occur or not");
        }
        Console.WriteLine("hi this is main function end");
    }
    }
    
}

Output:
hi this is main function start
It will always execute whether exception will occur or not
hi this is main function end

Explanation:
. Throw keyword is used to throw some exception whenever we want or whenever we have the possibility of getting exception, so there we can throw exception. 
.You can refer to above code for more clarification.


Some Important Concept regarding Exception Handling
Note1: We can write our own exception class but we have to inherit that class From Exception class. And 1 argument constructor is mandatory to write in that class. Please check the below code for more clarification:
Code:
using System;

namespace main_class{
    class DivideZeroException: Exception{
        
        public DivideZeroException(string message):base(message){
            
        }
        
    }
    class MainClass{
        static void Main(string[] args){
        Console.WriteLine("hi this is main function start");
        int a=10;
        int b=0;
        
        try{
        int div_result;
        if(b==0){
            throw (new DivideZeroException("you are dividing by zero please re enter the number b"));
        }
        //div_result=a/b;
        //Console.WriteLine("{0}",div_result);
        }
        catch(DivideZeroException e){
            Console.WriteLine("you are dividing by zero please re enter the number b");
        }

        catch(NullReferenceException e){
            Console.WriteLine("ypu are using a null reference object");
        }
        
        finally{
            Console.WriteLine("It will always execute whether exception will occur or not");
        }
        Console.WriteLine("hi this is main function end");
    }
    }
    
}

Output:
hi this is main function start
you are dividing by zero please re enter the number b
It will always execute whether exception will occur or not
hi this is main function end

    
     

No comments:

Working With Java Collections