1) Which is not a valid keyword used in the context of exception handling?
- try
- catch
- final
- finally
Correct answer: 3
final
The final keyword is not used to handle exceptions in C#.NET.
2) Which is a valid keyword used in the context of exception handling?
- through
- throw
- final
- caught
Correct answer: 2
throw
The throw is a valid keyword used in exception handling.
4) What is the correct output of the given code snippet?
using System;
namespace my_namespace
{
class program
{
static void Main(string[] args)
{
int a = 0;
int b = 10;
int c = 0;
try
{
c = b / a;
}
catch (DivideByZeroException d)
{
Console.WriteLine("Divide by zero exception occurred");
}
}
}
}
- Divide by zero exception occurred
- Syntax error
- Linker error
- No output
Correct answer: 1
Divide by zero exception occurred
The above code will print "Divide by zero exception occurred" on the console screen.
5) In the given code snippet finally block will execute or not?
using System;
namespace my_namespace
{
class program
{
static void Main(string[] args)
{
int a = 0;
int b = 10;
int c = 0;
try
{
c = b / a;
}
finally
{
Console.WriteLine("Finally executed");
}
}
}
}
- Yes, finally will execute
- No, finally will not execute
Correct answer: 1
Yes, finally will execute
Yes, finally block will execute.
The output would be,
Unhandled Exception:
System.DivideByZeroException: Attempted to divide by zero.
at my_namespace.program.Main (System.String[] args) [0x00007] in <64033460b1514282bc54bfc57fe0cb7f>:0
Finally executed
[ERROR] FATAL UNHANDLED EXCEPTION: System.DivideByZeroException: Attempted to divide by zero.
at my_namespace.program.Main (System.String[] args) [0x00007] in <64033460b1514282bc54bfc57fe0cb7f>:0