Try catch Implementation

Ersudeep13
Posted by Ersudeep13 under .NET Framework category on | Points: 40 | Views : 503
TRy--> this block contains all the statements in which there is possibility of exception occurance.
Catch--> this block contains all the statements to handle the exception that is raised within the try block.
Finally--> this block contains the statements to be executed compulsory, though try block is executed or catch block is executed.

A try block can be followed by any number of catch blocks,writting finally block is optinal.
writing an exception class with in the catch block is optional.
if catch block is used with out an exception class then it is known as generic catch block.
if catch block is used with an exception class then it is known as specific catch block.

Execution--> Execution starts from try block, if there any exception occurs in any statement of try block then following lines of exception statements are ignored and control jumps to catch block, catch block is executed and then finally block.
if no exception occurs in any statement of try block then all statements of try block are executed and catch is ignored then finally block is executed.

program of try catch block--

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace exeception
{
class Exception
{
static void Main(string[] args)
{
int a, b, c;
Console.Write("Enter any two numbers");
try
{
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = a / b;
Console.WriteLine("Quotient value is :-" + c);
}
catch
{
Console.WriteLine("Error occured");
}
finally
{
Console.WriteLine("Code Executed");
}
Console.Read();
}
}
}

Comments or Responses

Login to post response