Your Ad Here
Showing posts with label Exception. Show all posts
Showing posts with label Exception. Show all posts

Friday, May 30, 2008

Exception Handling

/* Simple Example On Exception Handling. This program is just intended to give you an idea of how an exception is handled by your C# program. */

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

namespace Program1
{
class Program
{
static void Main(string[] args)
{
double a;

try
{
Console.WriteLine(Enter A float Number);
a = Convert.ToDouble(Console.ReadLine());
}
catch (SystemException ex)
{
Console.WriteLine(ex);
}
}
}
}

Exception Handling - Addition Of Two Byte Numbers

/* C sharp program on exception handling. Addition of two byte numbers. */

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

namespace Program1
{
class check
{
public static void Main()
{
byte numOne;
byte numTwo;
byte result;

Console.WriteLine("Byte Size - (0-255)");
Console.Write("Enter a byte number : ");
numOne = Convert.ToByte(Console.ReadLine());
Console.Write("Enter a byte number : ");
numTwo = Convert.ToByte(Console.ReadLine());

try
{
checked
{
result = (byte) (numOne + numTwo);
}
Console.WriteLine("Result : " + result);
}

catch (ArithmeticException ex)
{
Console.WriteLine(ex);
}
}
}
}