http://www.dotnetperls.com/exception
Following program is given in the above website (3rd program). Without try and catch it is giving the same output. So what is the purpose of using try and catch.
using System;
using System.Collections;
class Program
{
static void Main()
{
//try
//{
// Create new exception.
var ex = new DivideByZeroException("Message");
// Set the data dictionary.
ex.Data["Time"] = DateTime.Now;
ex.Data["Flag"] = true;
// Throw it!
//throw ex;
//}
//catch (Exception ex)
//{
// Display the exception's data dictionary.
foreach (DictionaryEntry pair in ex.Data)
{
Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
}
//}
Console.ReadKey();
}
}
/*
Time = 18/09/2013 9:34:38 AM
Flag = True
*/
Loading
Posted Sep 19, 2013, 8:34 AM
Jignesh TrivediPosted Sep 18, 2013, 11:53 PM
hi,
When ever an exception is thrown, the CLR looks for catch block statement to handle the exception. If no catch block found than the CLR display unhandled exception message to user and stop execution of program.
In your code there is no execution so it give same output.
hope this will help you.
Posted Sep 18, 2013, 4:29 PM
Joxin StanlyPosted Sep 18, 2013, 10:30 AM
in the above example, the purpose of Data property is illustrated.
It gives developers flexibility to store additional data.
Hope this helps....
Abhishek JainPosted Sep 18, 2013, 10:01 AM
Regards
AJ