Welcome to Dream.In.Code
Become a C# Expert!

Join 150,390 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,064 people online right now. Registration is fast and FREE... Join Now!




try and catch

 
Reply to this topicStart new topic

try and catch, how to use try and catch

anishkumarv
3 Mar, 2008 - 03:22 PM
Post #1

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 7

please send ...............!
explaination of try and catch,and explain how to use in program

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Try And Catch
3 Mar, 2008 - 03:47 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,327



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You can find out about the Try/Catch and its usage here.
User is offlineProfile CardPM
+Quote Post

davegeek
RE: Try And Catch
4 Mar, 2008 - 12:27 AM
Post #3

D.I.C Head
Group Icon

Joined: 30 Jan, 2008
Posts: 81



Thanked: 2 times
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month