Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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 solution

Better is this code which makes more sense:
C#
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[^]
 
Share this answer
 
v4
Comments
shaikh-adil 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900