By 22/07/2015 13 Comments

C# Throw Statement

  • Some time run-time errors are occurred in our program, that run-time errors are called exception(s).
  • C# throw statement is used to throw an exception manually and it handles run-time errors in our code.


Example:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // int type variables are declared.
            int number, divider, result;

            // Variables are initialiazed.
            number = 10;
            divider = 0;
            result = 0;

            // Exception handling code block is declared.
            try
            {
                // if condition.
                if (divider == 0)
                {
                    // throw statement which throws a run-time error manually.
                    throw new DivideByZeroException("Any value cannot be divided by 0 !");

                }
                else
                {
                    // Performing division.
                    result = number / divider;

                    // Print result.
                    Console.WriteLine(number + " / " + divider + " = " + result);
                }
            }
            // Following catch block catches run-time error and handel it.
            catch (DivideByZeroException divideByZeroException)
            {
                // Print an error message.
                Console.WriteLine(divideByZeroException.Message);
            }
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class.

    /*
    The Output will be:
    Any value cannot be divided by 0 !
   */
}






← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .