Throw Exception from a function : Exception Throw « Language Basics « C# / CSharp Tutorial






using System;

class MainClass
{
    
    static void AFunction()
    {
        int Zero = 0;
        int j = 22 / Zero;
    }
    public static void Main()
    {
        try
        {
            Console.WriteLine("efore Function");
            AFunction();
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine("DivideByZero {0}", e);
        }
    }
}
efore Function
DivideByZero System.DivideByZeroException: Attempted to divide by zero.
   at MainClass.Main()








1.22.Exception Throw
1.22.1.Manually throw an exception.
1.22.2.Rethrow an exception
1.22.3.Throw Exception from a function
1.22.4.Wrap exception in another one, adding additional context
1.22.5.Throw Exception in finally statement
1.22.6.Creating and throwing an exception object