Net-informations.com
SiteMap  | About    

C# Exception handling

The C# language uses exceptions to handle errors and other exceptional events. Exceptions are the occurrence of some conditions that changes the normal flow of execution . Exceptions are occurred in situations like your program run out of the memory , file does not exist in the given path , network connections are dropped etc. More specifically for better understanding , we can say it as Runtime Errors occurs during the execution of a program that disrupts the normal flow of instructions

c# exception

In .NET languages , Structured Exceptions handling is a fundamental part of Common Language Runtime . All exceptions in the Common Language Runtime are derived from a single base class , also you can create your own custom exception classes. You can create an exception class that inherits from Exception class . Creating an exception object and handing it to the runtime system is called throwing an exception.

c# exception try catch finally

c# Exception Handling - try-catch

C# Exception handling uses the try, catch, and finally keywords to attempt actions that may not succeed, to handle failures, and to clean up resources afterwards.

  try
  {
	  //your code here
  }
  Catch (exception type)
  {
	  //your code here

}

finally

The code in the finally block will execute even if there is no Exceptions. That means if you write a finally block , the code should execute after the execution of try block or catch block.

  try
  {
	  //your code here
  }
  Catch (exception type)
  {
	  //if the exception occurred
	  //your code here
  }
  finally
  {
	  //your code here

}

The following example trying to divide a number by zero.

try { int val = 100; int div = 0; int resultVal; resultVal = (val / div); MessageBox.Show("The result is : " + resultVal); } catch (System.Exception ex) { MessageBox.Show("Exception catch here - details : " + ex.ToString()); } finally { MessageBox.Show("Enter finally block "); }

How to throw Exception

Exception objects that describe an error are created and then thrown with the throw keyword. By using a throw statement inside a catch block, we can change the resulting exception. More about.... throw exception

How to create a custom exception

If you want users to be able to programmatically distinguish between some error conditions, you should create your own custom exceptions. It will simplify and improve the error handling and thus increase the overall code quality. More about.... Create a custom exception

System level Exceptions Vs Application level Exceptions

Exceptions are provide a structured, uniform, and type-safe way of controlling both system level and application level abnormal conditions. It can be generated by system or can be generated programmatically. More about.... System level Exceptions VS ..

NullReferenceException

NullReferenceException indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized. More about.... NullReferenceException

Difference between Exception and Error

An exception is an Object of a type deriving from the System.Exception class. SystemException is thrown by the CLR (Common Language Runtime) when errors occur that are nonfatal and recoverable by user programs. More about.... Exception and Error

From the following C# code , you can understand how to use try..catch statements. Here we are going to divide a number by zero .




using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int val = 100;
                int div = 0;
                int resultVal;
                resultVal = (val / div);
                MessageBox.Show("The result is  : " + resultVal);
            }
            catch (System.Exception  ex)
            {
                MessageBox.Show("Exception catch here - details  : " + ex.ToString());
            }
            finally
            {
                MessageBox.Show("Enter finally block ");
            }
        }
    }
}

When you execute this C# code the above source code , the program will throw a DividedByZero Exception and after that the control wil go to finally clause.




net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.