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.
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);
}
}
}
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");
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
Answer 1:
Output:
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.
Answer 2:
Output:
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.
Answer 3:
Output:
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.
Answer 4:
Output:
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.
Answer 5:
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer