Q:

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

0

Find the output of C#.Net programs | Exception Handling | Set 2: Enhance the knowledge of C#.Net Exception Handling concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int num = 0;

        try
        {
            num = arr.GetValue(4);

            Console.WriteLine(num);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Question 2:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int num = 0;

        try
        {
            num = Convert.ToInt32(arr.GetValue(5));

            Console.WriteLine(num);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Question 3:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            DivideByZeroException e = new DivideByZeroException("My Exception");

            through e;
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Question 4:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            DivideByZeroException e = new DivideByZeroException("My Exception");

            throw e;
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

Question 5:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        try
        {
            DivideByZeroException e = new DivideByZeroException();
            e.Message = "Hello Program";

            throw e;
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

main.cs(13,23): error CS0266: Cannot implicitly convert type `object' to `int'. 
An explicit conversion exists (are you missing a cast?)

Explanation:

The above program will generate a syntax error because of the below statement,

num = arr.GetValue(4);

In the above program, GetValue() method return the value of object type and variable num integer. Here, we need to use Convert.ToInt32() method to convert object value into integer value.

Answer 2:

Output:

Index was outside the bounds of the array.
Press any key to continue . . .

Explanation:

In the above program, we created an array arr of 5 integers and a variable num,

The above program will throw a runtime exception because we accessed items from an array on index 5, which does not exist in the array. In the above program, the highest index of the array is 4.

The generated exception caught by Exception class then message "Index was outside the bounds of the array" will be printed on the console screen.

Answer 3:

Output:

main.cs(12,21): error CS0128: A local variable named `e' is already defined in this scope

Explanation:

The above program will generate syntax error because through is not a built-in keyword in C#.

Answer 4:

Output:

My Exception
Press any key to continue . . .

Explanation:

In the above program we created object e of DivideByZeroException class initialized with the string "My Exception" then we throw exception using throw statement that will be caught by catch block and print "My Exception" on the console screen.

Answer 5:

Output:

main.cs(11,15): error CS0200: Property or indexer `System.Exception.Message' 
cannot be assigned to (it is read-only)

Explanation:

The above program will generate a syntax error because of the below statement,

e.Message = "Hello Program";

In the above statement, we tried to assign the "Hello Program" string in the "Message" property but "Message" is the read-only property that cannot be assigned.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now