using System;
using System.IO;
//Exception Handling
// is an unforeseen error that occurs when a program is running.
//An exception is actually a class that derives from System.Exception class
class ExceptionHandling
{
static void Main(string[] args)
{
StreamReader streamReader = null;
try
{
streamReader = new StreamReader(@"C:\Data.txt");
Console.WriteLine(streamReader.ReadToEnd());
}
//catch(Exception ex)
catch (FileNotFoundException ex) //specific exception
{
//Log the details to the DB
Console.WriteLine("Please check if the file {0} exists", ex.FileName);
//Console.WriteLine(ex.Message);
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine(ex.StackTrace);
}
catch (Exception ex) //general exception
{
Console.WriteLine(ex.Message);
}
finally
{
//finally excute whatever happening before
if (streamReader != null)
{
streamReader.Close();
}
}
}
}
===================================
Inner Exception property returns the exception instance that caused the current exception
using System;
using System.IO;
class InnerException
{
static void Main(string[] args)
{
try
{
try
{
Console.WriteLine("Enter First Number");
int FN = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number");
int SN = Convert.ToInt32(Console.ReadLine());
int Result = FN / SN;
Console.WriteLine("Result = {0}", Result);
}
catch (Exception ex)
{
string filePath = @"C:\log.txt";
if (File.Exists(filePath))
{
StreamWriter sw = new StreamWriter(filePath);
sw.Write(ex.GetType().Name);
sw.WriteLine();
sw.Write(ex.Message);
sw.Close();
Console.WriteLine("There is a problem, please try later");
}
else
{
throw new FileNotFoundException(filePath + " is not present", ex);
}
}
}
catch (Exception exception)
{
Console.WriteLine("Current Exception = {0}", exception.GetType().Name);
if (exception.InnerException != null)
{
//prevent from devided by zero
Console.WriteLine("Inner Exception = {0}", exception.InnerException.GetType().Name);
}
}
}
}
=============================
Custom Exception
using System;
using System.IO;
using System.Runtime.Serialization; // for [Serializable]
/* Custom Exception
* 1. Create a class that derives from System.Exception class
* 2. Provide a public constructor that takes in a string parameter
* 3. using InnerExceptions, you can also track back the original exception
* 4. If you want your Exception class object to work across application domains,
* then the object must be serializable.
* */
class CustomExceptionsDemo
{
static void Main()
{
try
{
throw new UserAlreadyLoggedInException("User is logged in - no duplicate session allowed");
}
catch (UserAlreadyLoggedInException ex)
{
Console.WriteLine(ex.Message);
}
}
}
[Serializable]
public class UserAlreadyLoggedInException : Exception
{
public UserAlreadyLoggedInException()
: base()
{
}
public UserAlreadyLoggedInException(string message) : base(message)
{
}
public UserAlreadyLoggedInException(string message, Exception innerException)
: base(message, innerException)
{
}
public UserAlreadyLoggedInException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
=======================================
example of exception
using System;
public class ExceptionHandlingAbuse
{
public static void Main()
{
try
{
Console.WriteLine("Please enter Numerator");
int Numerator;
bool IsNumeratorConversionSuccessful = Int32.TryParse(Console.ReadLine(), out Numerator);
if (IsNumeratorConversionSuccessful)
{
Console.WriteLine("Please enter Denominator");
int Denominator;
bool IsDenominatorConversionSuccessful = Int32.TryParse(Console.ReadLine(), out Denominator);
if (IsDenominatorConversionSuccessful && Denominator != 0)
{
int Result = Numerator / Denominator;
Console.WriteLine("Result = {0}", Result);
}
else
{
if (Denominator == 0)
{
Console.WriteLine("Denominator cannot be zero");
}
else
{
Console.WriteLine("Denominator should be a valid number between {0} && {1}", Int32.MinValue, Int32.MaxValue);
}
}
}
else
{
Console.WriteLine("Numerator should be a valid number between {0} && {1}", Int32.MinValue, Int32.MaxValue);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}