Professional Documents
Culture Documents
C# Exception
Exceptions are runtime errors that can make a program behave abruptly if not handled in the right way.
Hence, most programming languages add exception handling capabilities. C# too has a robust object-oriented
exception handling framework. And a good C# programmer should know how to use C# exception handling
techniques to ensure good user experience.
A programmer must be careful while handling exceptions in the application code. It could be a background
service where you need to log errors in a file or an interactive user application expecting for a proper error
message. There might even cases when you have to catch and rethrow an exception. For example, rethrowing
an exception could be useful in rolling back a database transaction.
Following are some of the key facts that you should know about exception handling in C#.
In C# language, Exception (<System.Exception>) is the base class of all exceptions. And it itself is derived
from the Object (<System.Object>) class.
Other exception classes like <System.ApplicationException> and <System.SystemException> derive directly
or indirectly from the Exception class.
You can also create a user-defined or a custom exception in C#. With custom exceptions, you can decode a
complex error into a meaningful message.
Exception handling in C# mainly revolves around the four keywords.
// Try block
try
{
// Program instructions Block.
}
// Catch block
catch (ExceptionType e)
{
// Instructions to handle exception.
}
// Finally block
finally
{
// Instructions to clean up.
}
// Throw keyword
catch (Exception ex)
{
throw (ex);
}
A try block can cause more than one exceptions. By having a catch block without brackets, you can handle
them all within the same catch block.
To rethrow an exception, you can use the <throw> keyword.
User-defined catch block must appear before any generic catch block. Otherwise, the runtime will ignore it
from execution.
In the absence of an exception handler, the program will abort with an error message.
using System;
namespace Null_Reference_Exception
{
public class Program
{
public static void Main()
{
string[] list = new string[5];
list[0] = "Sunday";
list[1] = "Monday";
list[2] = "Tuesday";
list[3] = "Wednesday";
list[4] = "Thursday";
for (int i = 0; i <= 5; i++)
{
Console.WriteLine(list[i].ToString());
}
Console.ReadLine();
}
}
}
Instructions: Encode the source code, compile and run. If problems encountered write the errors (explain) and
rewrite (debug) the code and add exception handling to catch the specific exception.
using System;
namespace Overflow_Exception
{
public class Program
{
public static void Main()
{
int num1, num2;
byte result;
num1 = 30;
num2 = 60;
result = Convert.ToByte(num1 * num2);
Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
Console.ReadLine();
}
}
}
Instructions: Encode the source code, compile and run. If problems encountered write the errors (explain) and
rewrite (debug) the code and add exception handling to catch the specific exception.
using System;
try{
f =
Convert.ToDouble("1235555555555555555555555555555555555555555555555555555555555555555555555
55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555
55");
}
catch (FormatException){
Console.WriteLine("We got a FormatException");
}
catch (OverflowException){
Console.WriteLine("We got an OverflowException");
}
catch (Exception){
Console.WriteLine("We got another Exception");
}
Console.WriteLine("{0} {1} {2}", d, e, f);
}
}
Instructions: Encode the source code, compile and run. Write the output of the source code and explain what
causes the program to display the output. (Screen Capture and Explanations)
Note: Use the Document File (Last Name First Name MI – HomeActivity1.docx) included in the ELMS and
upload your solutions in one folder and named it HomeActivity1XXX.rar or (.zip). Replace XXX with our initials.