Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Exception Handling) | set 3
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.
Submitted by Nidhi, on February 10, 2021
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);
}
}
}
Output:
My Exception generated
Press any key to continue . . .
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.
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);
}
}
}
Output:
Exception of type 'MyException' was thrown.
Press any key to continue . . .
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.
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);
}
}
}
Output:
main.cs(8,14): error CS0200: Property or indexer `System.Exception.Message'
cannot be assigned to (it is read-only)
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.
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");
}
}
}
Output:
main.cs(23,8): error CS1525: Unexpected symbol `{'
Explanation:
The above program will generate because we use final instead of finally. final is not any built-in keyword in C#.
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");
}
}
}
Output:
65
program finished
Press any key to continue . . .
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.