EXCEPTION HANDLING IN C#
using System;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
public static void Main()
{
try
{
StreamReader streamreader = new StreamReader(@"C:\Users\mallareddy\OneDrive\Documents1\Data.txt");
Console.WriteLine(streamreader.ReadToEnd());
streamreader.Close();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
// More specific exception at first, if you place general exception first then it throughs exception as
// a previous catch clause already catches all exceptions of this or of a super type('System.Exception')
catch (FileNotFoundException ex)
{
//Console.WriteLine(ex.Message);
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine(ex.StackTrace);
Console.WriteLine("Please check if the file exist = {0}", ex.FileName);
Console.ReadLine();
}
// General exception at the bottom
}
}
}
===================================================================
CLASS HIERARCHY EXCEPTIONS
TRY , CATCH, CATCH..
using System;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
public static void Main()
{
try
{
StreamReader streamreader = new StreamReader(@"C:\Users\mallareddy\OneDrive\Documents1\Data.txt");
Console.WriteLine(streamreader.ReadToEnd());
streamreader.Close();
Console.ReadLine();
}
catch (FileNotFoundException ex)
{
//Console.WriteLine(ex.Message);
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine(ex.StackTrace);
Console.WriteLine("Please check if the file exist = {0}", ex.FileName);
Console.ReadLine();
}
// General exception at the bottom
catch (Exception ex)
{
ConsolCe.WriteLine(ex.Message);
Console.ReadLine();
}
// More specific exception at first, if you place general exception first then it throughs exception as
// a previous catch clause already catches all exceptions of this or of a super type('System.Exception')
}
}
}
=======================================================================
COMPLETE EXCEPTION HANDLING
using System;
using System.IO;
namespace ConsoleApplication4
{
class Program
{
public static void Main()
{
StreamReader streamreader = null;
try
{
streamreader = new StreamReader(@"C:\Users\mallareddy\OneDrive\Documents\Data.txt");
Console.WriteLine(streamreader.ReadToEnd());
Console.ReadLine();
}
catch (FileNotFoundException ex)
{
//Console.WriteLine(ex.Message);
//Console.WriteLine();
//Console.WriteLine();
//Console.WriteLine(ex.StackTrace);
Console.WriteLine("Please check if the file exist = {0}", ex.FileName);
Console.ReadLine();
}
// General exception at the bottom
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
// More specific exception at first, if you place general exception first then it throughs exception as
// a previous catch clause already catches all exceptions of this or of a super type('System.Exception')
finally
{
if (streamreader != null)
{
streamreader.Close();
Console.WriteLine("Finally Block");
Console.ReadLine();
}
}
}
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.