try and catch

how to use try and catch

Page 1 of 1

2 Replies - 3657 Views - Last Post: 04 March 2008 - 01:27 AM Rate Topic: -----

#1 anishkumarv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 27-February 08

try and catch

Posted 03 March 2008 - 04:22 PM

please send ...............!
explaination of try and catch,and explain how to use in program
Is This A Good Question/Topic? 0
  • +

Replies To: try and catch

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: try and catch

Posted 03 March 2008 - 04:47 PM

You can find out about the Try/Catch and its usage here.
Was This Post Helpful? 0
  • +
  • -

#3 davegeek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 81
  • Joined: 30-January 08

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1