Catch Divide By Zero Exception : Exception Try Catch « Language Basics « C# / C Sharp






Catch Divide By Zero Exception

Catch Divide By Zero Exception
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

namespace nsDivZero
{
    using System;
    public class DivZero
    {
        static public void Main ()
        {
            // Set an integer equal to 0
            int IntVal1 = 0;
            // and another not equal to zero
            int IntVal2 = 57;
            try
            {
                Console.WriteLine ("{0} / {1} = {2}", IntVal2, IntVal1, IntResult (IntVal2, IntVal1) / IntResult (IntVal2, IntVal1));
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine (e.Message);
            }
            // Set a double equal to 0
            double dVal1 = 0.0;
            double dVal2 = 57.3;
            try
            {
                Console.WriteLine ("{0} / {1} = {2}", dVal2, dVal1, DoubleResult (dVal2, dVal1));
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine (e.Message);
            }
        }
        static public int IntResult (int num, int denom)
        {
            return (num / denom);
        }
        static public double DoubleResult (double num, double denom)
        {
            return (num / denom);
        }
    }
}


           
       








Related examples in the same category

1.Catch Error
2.illustrates an unhandled exceptionillustrates an unhandled exception
3.illustrates how to handle a specific exceptionillustrates how to handle a specific exception
4.illustrates multiple catch blocksillustrates multiple catch blocks
5.illustrates a nested try/catch blockillustrates a nested try/catch block
6.illustrates exception propagation with methodsillustrates exception propagation with methods
7.Demonstrates stacking catch blocks to provide alternate code for more than one exception type
8.Catches an exception that was thrown in a component
9.Throw a format exception purposely to demonstrate catching a FormatException
10.Demonstrates using if statements to sort out an IOException
11.Several catch branchesSeveral catch branches
12.Demonstrate exception handlingDemonstrate exception handling
13.An exception can be generated by one method and caught by anotherAn exception can be generated by one 
   method and caught by another
14.Exception Type MismatchException Type Mismatch
15.Handle error gracefully and continueHandle error gracefully and continue
16.Use multiple catch statementsUse multiple catch statements
17.Use the 'catch all' catch statementUse the 'catch all' catch statement
18.Use a nested try blockUse a nested try block
19.Catch different exceptions
20.Passing Exceptions on to the Caller: Caller Confuse
21.Passing Exceptions on to the Caller: Caller Inform
22.Exception Handling:Trying and CatchingException Handling:Trying and Catching