C#.Net find output programs (Exception Handling) | set 1

Find the output of C#.Net programs | Exception Handling | Set 1: Enhance the knowledge of C#.Net Exception Handling concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 10, 2021

Question 1:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            int A = 10;
            int B = 0;
            int C = 0;

            C = A / B;

            Console.WriteLine("C: " + C);
        }
    }
}

Output:

main.cs(18,4): error CS1524: Unexpected symbol `}', expecting `catch' or `finally'

Explanation:

The above program will generate syntax error because if we are using the try block then it is mandatory to define catch or finally block in the program.

Question 2:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            int A = 10;
            int B = 0;
            int C = 0;

            C = A / B;

            Console.WriteLine("C: " + C);
        }
        finally
        {
            Console.WriteLine("Finally executed");
        }
    }
}

Output:

Unhandled Exception:
System.DivideByZeroException: Attempted to divide by zero.
  at Program.Main (System.String[] args) [0x00007] in <b772be7e684744049f5a145d5d5e1c1d>:0
Finally executed
[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Attempted to divide by zero.
  at Program.Main (System.String[] args) [0x00007] in <b772be7e684744049f5a145d5d5e1c1d>:0

Explanation:

The above program will generate runtime exception due to divide by zero. In the above program, we declared three local variables A, B, and C initialized with 10, 0, and 0 respectively.

C = A/B;
C = 10/0;

The above statement will generate runtime exception and we did not handle exception in our program that's why the program gets crashed and then finally block gets executed and print "Finally executed" on the console screen.

Question 3:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            int A = 10;
            int B = 0;
            int C = 0;

            C = A / B;

            Console.WriteLine("C: " + C);
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }

    }
}

Output:

Attempted to divide by zero.
Press any key to continue . . .

Explanation:

In the above program, we declared three local variables A, B, and C initialized with 10, 0, and 0 respectively.

C = A/B;
C = 10/0;

The above statement will throw a divide by zero exception that will be caught by the catch block. Then Message property of DivideByZeroException class will print "Attempt to divide by zero" message on the console screen.

Question 4:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            int A = 10;
            int B = 0;
            int C = 0;

            C = A / B;

            Console.WriteLine("C: " + C);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Output:

main.cs(22,9): error CS0160: A previous catch clause already catches all 
exceptions of this or a super type `System.Exception'

Explanation:

The above program will generate syntax error because here we used two catch blocks but catch (Exception e) will catch all type exception because Exception is the superclass for all type of exception, but we also define catch (DivideByZeroException e) block, which is not required. That's why the above program will generate a syntax error.

Question 5:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            int A = 10;
            int B = 0;
            int C = 0;

            C = A / B;

            Console.WriteLine("C: " + C);
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Output:

Attempted to divide by zero.
Press any key to continue . . .

Explanation:

In the above program, we declared three local variables A, B, and C initialized with 10, 0, and 0 respectively.

The above statement will throw divide by zero exception that will be caught by the catch block. Then Message property of DivideByZeroException class will print "Attempt to divide by zero" message on the console screen.

Here, catch (Exception e) block is not required because the above code can generate only divide by zero exception if any other type of exception generated by the program that will be caught by Exception class.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.