EXCEPTION in c#
#EXCEPTION :-
1. Exception is returning logically error .
2. There are following types of exception used in c#
programming that are listed below.
3. All types of exception are stored in exception class.
A. Try Catch
B. Finally
C. Throw
D. User define exception.
A. Try catch :-
1. TRY:- It represented the program logic .
2. CATCH :- It return the logical exception.
Progrm:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace progexcepetion2
{
class Program
{
void input(int a , int b)
{
int c;
try
{
c = a / b;
Console.WriteLine(c);
}
catch(Exception obj)
{
Console.WriteLine(obj);
}
}
static void Main(string[] args)
{
Program p1 = new Program ();
p1.input(a: 10,
b:2);
Console.ReadKey();
;
}
}
}
* How to pass
user mess in exception??
Ans. In this
concept using throw exception .
Syntax:-
throw new exception (“mess”)
Prog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace progthrow
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 2;
int c;
try
{
if (b == 0)
{
throw new Exception("Enter valid
no.");
}
c = a / b;
Console.WriteLine(c);
}
catch (Exception obj)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
}
3. Finally:-
1. Finally it is used to represent comment mess .
2. Finally is not related to exception.
Prog
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace progfinallyexception
{
class Program
{
static void Main(string[] args)
{
int a = 10; int b = 1; int c;
try
{
c = a / b;
Console.WriteLine(c);
}
catch(Exception obj)
{
Console.WriteLine(obj);
}
finally
{
Console.WriteLine("it is division program");
}
Console.ReadKey();
}
}
}
Comments
Post a Comment