Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
namespace ConsoleApplication1
{
    class myexception : Exception
    {
        public myexception(string str)
        {
            Console.WriteLine("user define exception" + str);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new myexception("aquib");
            }
            catch (Exception e)
            {
                Console.WriteLine("exeption caught here" + e.ToString());
            }
            Console.WriteLine("last statement");
            Console.ReadKey();
        }
    }
}
what will this code will do?
i know this is exception handling but how this will works?
i search on web. but its too dificult to understand
can anyone help me out
thanks in advance
Posted 1-Oct-12 23:32pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Better is this code which makes more sense:
namespace ConsoleApplication1
{
    class myexception : Exception
    {
        public myexception(string str)
        {
            Console.WriteLine("user define exception" + str);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //do something
                //...
                throw new myexception("aquib");
                //following code is not executed
            }
            catch (myexception e)
            {
                Console.WriteLine("myexception caught here" + e.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("general exception caught here" + e.ToString());
            }
            Console.WriteLine("last statement");
            Console.ReadKey();
        }
    }
}
Using the above code you are able to differentiate between your exception being caught or general exceptions that e.g. the .NET framework throws.
You should throw your own exceptions in case you determine in your functions that something was bad.
 
The program flow is as follows:The "main" function is entered and when the exception is thrown, the code that is directly after that is not executed since it jumps to the catch block that is appropriate. In the case above it jumps to "catch(myexception e)". After handling the exception it can continue with the next code. In your case it writes to the output "last statement".
 
Please read these articles for some more information:
Exception Handling Best Practices in .NET[^]
User Friendly Exception Handling[^]
  Permalink  
v4
Comments
qureshiaquib at 2-Oct-12 5:39am
   
hey jf bro.
i dont know about the program flow.
can your explain first?
plz
you can explain your program
it is far more understandable
Sergey Alexandrovich Kryukov at 2-Oct-12 22:17pm
   
This is just too fundamental topic to explain in one answer. Many developers would "understand" how the code is working, having no clue on the value of exceptions, forget the mechanism. It's too hard to explain things in a black-box style, because exceptions go above the usual call-and-return stack-based mechanism. Only people who learned how the stack, stack frames, parameter passing, call and return, and, hopefully, thread work at low level can really understand how they work and how to use them properly. I almost never see the proper use in the samples one this side (which is, actually, mean very, very sparse use of exception handlers, to start with). From the other head, exceptions is one of the greatest discoveries; I value them more then, say, the whole OOP. Well, we are coming into the epoch when "developers" understand nearly nothing. :-<
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 Richard Deeming 215
3 CHill60 170
4 _Amy 145
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,067
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 2 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100