Try these code snippets also:
CODE
class Debugging
{
static string[] eTypes = { "none", "simple", "index", "nested index" };
static void ThrowException(string exceptionType)
{
Console.WriteLine("Throw exception (\"{0}\") reached" + exceptionType);
switch (exceptionType)
{
case "none":
Console.WriteLine("Not throwing an exception");
break;
case "simple":
Console.WriteLine("Throwing System.Exception");
throw(new System.Exception());
break;
case "index":
Console.WriteLine("Throwing System.IndexOutOfRangeException.");
eTypes[4] = "error";
break;
case "nested index":
try
{
Console.WriteLine("ThrowException(\"nested index\") " + "try block reached.");
Console.WriteLine("ThrowException(\"index\") called.");
ThrowException("index");
}
catch
{
Console.Write("ThrowException(\"nested index\") general" + " catch block reached.");
}
finally
{
Console.WriteLine("ThrowException(\"nested index\") finally" + " block reached.");
}
break;
}
}
static void Main(string[] args)
{
foreach (string eType in eTypes)
{
try
{
Console.WriteLine("Main() try block reached.");
Console.WriteLine("ThrowException(\"{0}\") called.", eType);
ThrowException(eType);
Console.WriteLine("Main() try block continues.");
}
catch (System.IndexOutOfRangeException e)
{
Console.WriteLine("Main() System.IndexOutOfRangeException catch" + " block reached. Message:\n\"{0}\"", e.Message);
}
catch
{
Console.WriteLine("Main() general catch block reached.");
}
finally
{
Console.WriteLine("Main() finally block reached.");
}
Console.WriteLine();
}
}
}
________________________________________________________________________________
_____________________________________________
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100 / x;
Console.WriteLine("This line in not executed");
}
catch (DivideByZeroException de)
{
Console.WriteLine("Exception occured");
}
finally
{
Console.WriteLine("Finally Block");
}
}
}
________________________________________________________________________________
_____________________________________________
public class ThrowTest
{
public static void Main()
{
string s = null;
if (s == null)
{
throw(new ArgumentNullException());
}
Console.Write("The string s is null"); // not executed
}
}
________________________________________________________________________________
___________________________________________________________
using System.IO;
FileStream objStream = new FileStream("C:\\AppLog.txt", FileMode.OpenOrCreate);
TextWriterTraceListener objTraceListener = new TextWriterTraceListener(objStream);
Trace.Listeners.Add(objTraceListener);
Trace.WriteLine("This is first trace message");
Trace.WriteLine("This is second trace message");
Debug.WriteLine("This is first debug message");
Trace.Flush();
objStream.Close();
This post has been edited by davegeek: 4 Mar, 2008 - 12:29 AM