Find the output of C#.Net programs | Exception Handling | Set 3: 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 MyException : Exception
{
// Constructor
public MyException() : base("My Exception generated")
{
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
try
{
MyException X = new MyException();
throw X;
}
catch (MyException e)
{
Console.WriteLine(e.Message);
}
}
}
Question 2:
using System;
class MyException : Exception
{
// Constructor
public MyException()
{
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
try
{
MyException X = new MyException();
throw X;
}
catch (MyException e)
{
Console.WriteLine(e.Message);
}
}
}
Question 3:
using System;
class MyException : Exception
{
// Constructor
public MyException()
{
this.Message = "My Exception Generated";
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
try
{
MyException X = new MyException();
throw X;
}
catch (MyException 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;
double D = 0.0;
D = (double)A;
Console.WriteLine(D);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
final
{
Console.WriteLine("program finished");
}
}
}
Question 5:
using System;
class Program
{
//Entry point of the program
static void Main(string[] args)
{
try
{
char A='A';
double D = 0.0;
D = (double)A;
Console.WriteLine(D);
}
catch (InvalidCastException e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("program finished");
}
}
}
Answer 1:
Output:
Explanation:
In the above program, we created the MyException class which is inherited by the Exception class. Here, we defined a no-argument constructor and call base class constructor with the message "My Exception generated" using the base keyword.
Now look to the Main() method, here we created object X of MyException class and throw an exception that will be caught by catch block and print "My Exception generated" message on the console screen.
Answer 2:
Output:
Explanation:
In the above program, we created the MyException class which is inherited by the Exception class. Here, we defined a no-argument constructor with an empty body.
Now look to the Main() method, here we created object X of MyException class and throw an exception that will be caught by catch block and print "Exception of type 'MyException' was thrown" message on the console screen because we did not assign any message to the Message property.
Answer 3:
Output:
Explanation:
The above program will generate syntax errors because of the below statement,
this.Message = "My Exception Generated";
In the above statement, we assigned a string message to the read-only property.
Answer 4:
Output:
Explanation:
The above program will generate because we use final instead of finally. final is not any built-in keyword in C#.
Answer 5:
Output:
Explanation:
In the above program, we two local variables A and D initialized with 'A' and 0.0 respectively.
D = (double)A; Console.WriteLine(D);
In the above statement, we typecast the value variable A into double and assigned to 'D', then it will print the ASCII value of 'A', that is 65 on the console screen.
After that, finally block gets executed and print "program finished" message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer