please send ...............!
explaination of try and catch,and explain how to use in program
try and catchhow to use try and catch
Page 1 of 1
2 Replies - 3241 Views - Last Post: 04 March 2008 - 01:27 AM
Replies To: try and catch
#3
Re: try and catch
Posted 04 March 2008 - 01:27 AM
Try these code snippets also:
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: 04 March 2008 - 01:29 AM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|