How to raise an Exception ?

 Posted by Bharathi Cherukuri on 7/25/2012 | Category: ASP.NET Interview questions | Views: 1698 | Points: 40
Answer:

An exception can be raised using th Throw keyword. You have to use this try statement within your exception-handling structure, which passes the control flow to the catch statement when an exception occurs.

Example:


using System;
class HandledException
{
public static void Main()
{
int x = 0;
int intTemp = 0;
try
{
intTemp = 100/x;
Console.WriteLine(?Program terminated before this statement?);
}
catch(DivideByZeroException de)
{
Console.WriteLine("Division by Zero occurs");
}
finally
{

Console.WriteLine("Result is {0}", intTemp);
}
}
}


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response