While writing and running the program there might be three types of error.
SYNTAX:-- Compile Time Error:-This error generally occurs because of syntax error.
- Run Time Error(Exception):- This error occurs when application is running but some logic is failed according to CLR.
- Logical Error:-Program is compile and running successfully but we are not getting the expected output due to not written correct logic.
Exception Handling:-In Exception handling we provide an alternate path that if some error occurs at run time instead of halting the program.some alternate path of code executed.There are some keywords which is used in exception handling.
- Try:- We put those codes to try block which may give some error at the run time.
- Catch:-A catch block is used for catch the Exception of try block codes if any.We can use multiple catch block for handling the Exception of one try block.
- Throw:-When any error is found in program then this keyword is used to throw the Exception to other block(method).
- Finally:-Finally block codes are always executed if any Exception is throw or not throw.This set of statement is always executed.
try
{
....statement....
}
catch(Exception ex)
{
....statement....
}
catch(Exception e)
{
....statement....
}
finally
{
....statement...
}
Exception classes in C#
There are some exception classes which is used for handling the Exception.All the Exception classes is derived from the two main classes.
- System.SystemException --->Predefined System Exception.
- System.ApplicationException -->Application program Exception.
These two classes are used for Exception handling in C#.some derived Exception classes are:-
- FormatException
- DivideByZeroException
- NullReferenceException
- ArgumentNullException
- NotFiniteNumberException
- ArrayTypeMismatchException
- ArgumentOutOfRangeException
- ContextMarshalException
- StackOverflowException etc.
Step :1 First open your visual studio --.File --> New--> Project-->Console Application-->click OK --> and write the following codes which is given below:
see it:-
using System;
namespace Exception_handling
{
class Program
{
class student
{
int res;
public void display(int a, int b)
{
try
{
res = a / b;
Console.WriteLine(res);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.Message);
}
catch (FormatException ex)
{
Console.WriteLine(ex.Message);
}
catch
{
Console.WriteLine("any other exception");
}
finally
{
Console.WriteLine("Press enter for Exit");
}
}
}
static void Main(string[] args)
{
student st = new student();
Console.WriteLine("Enter the two value");
int m = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());
st.display(m,n);
Console.ReadLine();
}
}
}
Step :2 Now Run the Program(press F5).see output:
Note:-This is example of predefined Exception class.
Create User defined Exception (Use of throw keyword):-
User defined Exception classes are derived from the Application Exception class.Which is shown in example which is given below:- follow same steps as above.
using System;
namespace userdefined_exception
{
class Program
{
class billingException:ApplicationException
{
public billingException():base("value can not be less than 1000")
{
}
public int calculate(int amount)
{
int total;
if (amount < 1000)
{
throw new billingException();
}
else
{
total = amount - 1000;
Console.WriteLine("amount greater than 1000 is: "+total );
}
return total;
}
}
static void Main(string[] args)
{
try
{
billingException obj = new billingException();
Console.WriteLine("Enter one amount value");
int m = int.Parse(Console.ReadLine());
obj.calculate(m);
}
catch (billingException ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
finally
{
Console.WriteLine("press enter for exit");
Console.ReadLine();
}
}
}
}
Output:-
- If you enter amount less than 1000 then you will see this output which is given below:
2. If you enter amount less than 1000 then you will see this output which is given below:
Note :-On the basis of this example you will create your own Exception which you want to show to your users.
If you want to run any specific project in visual studio then ,click Solution Explorer--> Right click on particular Project-->Set as Start UP Project.
See it:
For more:
- Partial Classes
- Operators in C#
- Microsoft sql server
- Basic elements are used for compiling the c# code.
- oops
- Data Integrity
I hope this is helpful for you.
To Get the Latest Free Updates Subscribe
Click below for download whole application.Please share these post to another friends.
Download
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.
ReplyDeletec# exception handling
ling