Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 8193 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > Exception Handling Scenarios in .NET

Exception Handling Scenarios in .NET

2 vote(s)
Rating: 5 out of 5
Article posted by Kishork80 on 8/9/2012 | Views: 2398 | Category: .NET Framework | Level: Intermediate | Points: 250 red flag

Advertisements

Advertisements
This article explains the exception handling scenario in .NET.

Objective


To handle errors in different scenarios.

Scenario 1: If the all the code lies in the same class there is no multilevel class functions.

Scenario 2: If the code lies in the different class there is multilevel class functions.


Using the code

 

**************************************************************************

Exception Handling Scenarios

**************************************************************************

Scenario 1: If the all the code lies in the same class there is no multilevel class functions.

//same code in class Program

//Program class

static int Test2()

        {

            int i = 0;

            int j = 0;

            try

            {

                j = i / i;

            }

            catch

            {

                throw ;

            }

            return j;

        }

        static void Main(string[] args)

        {

            try

            {

                Console.WriteLine(Program.Test2());

            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.Read();

        }

ERROR :

   at ConsoleApplication2.Program.Test2() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 20

   at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 30

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//same code in class Program

static int Test2()

        {

            int i = 0;   int j = 0;

            try    {

                j = i / i;

            }

            catch(Exception e)

       {

                throw e;

            }

            return j;

        }

        static void Main(string[] args)

        {

            try

            {

                Console.WriteLine(Program.Test2());

            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.Read();

        }

ERROR :

   at ConsoleApplication2.Program.Test2() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 20

   at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 30

Conclusion:

If the code lies in the same class there is no difference the way we put the try…catch block. The caller method (Main in this case) must have below code.

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

            }

 

**************************************************************************

 

Scenario 2: If the code lies in the different class there is multilevel class functions.

//Program class

static void Main(string[] args)

        {

            try

            {

                Console.WriteLine(Class1.TestTest());

            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.Read();

        }

 

//another class which has two functions

 class Class1

    {

      public static int TestTest()

        {

            try

            {

                return Class1.Test();// call the below function here

            }

            catch(Exception e)

            {

                throw e;

            }

   }

      public  static int Test()

        {

            int i = 0;

            int j = 0;

            try

            {

                j = i / i;

            }

            catch

            {

                throw;

            }

            return j;

        }

}

   at ConsoleApplication2.Class1.TestTest() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Class1.cs:line 18

   at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 29

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

static void Main(string[] args)

        {

            try

            {

                Console.WriteLine(Class1.TestTest());

            }

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.Read();

        }

//another class

 class Class1

    {

public static int TestTest()

        {

            try

            {

                return Class1.Test();// call the below function here

            }

            catch

            {

                throw ;

            }

         

        }

      public  static int Test()

        {

            int i = 0;

            int j = 0;

            try

            {

                j = i / i;

            }

            catch

            {

                throw;

            }

            return j;

        }

}

 

   at ConsoleApplication2.Class1.Test() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Class1.cs:line 32

   at ConsoleApplication2.Class1.TestTest() in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Class1.cs:line 18

   at ConsoleApplication2.Program.Main(String[] args) in C:\Documents and Settings\m9jodu\Local Settings\Application Data\Temporary Projects\ConsoleApplication2\Program.cs:line 29

Conclusion:

If the code lies in the multilevel classes there is a difference the way we put the try…catch block. The caller method (Main in this case) must have below code as usual.

            catch(Exception e)

            {

                Console.WriteLine(e.Message);

            }

If we put do not put  catch(Exception e) in the lower levels functions. The Caller Function (Main Here) gives all the stack trace from where it is originated or else you will lose stack trace. The generated error propagates and bubble up to the caller function.

 catch

            {

                throw;

            }

 

Conclusion

If we put do not put  catch(Exception e) in the lower levels functions. The Caller Function (Main Here) gives all the stack trace from where it is originated or else you will lose stack trace. The generated error propagates and bubble up to the caller function.


Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About kishor kumar

Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, July 01, 2010
Level:Starter
Status: [Member]
Biography:Having 6 pyears of exp in dot net and still counting...
>> Write Response - Respond to this post and get points
Related Posts

This article describes the features and enhancements that has happened with respect to Microsoft .NET Frameworks

In this article, we will try to understand three important concepts: association, aggregation, and composition. We will also try to understand in what kind of scenarios we need them. These three concepts have really confused a lot of developers and in this article; my attempt would be to present the concepts in a simplified manner with some real world examples.

This article explores the feature (of SQL server) of returning the multiple resultsets and handling such multiple resultsets in C# with SQL Data reader through inline SQL and as well as through stored procedure.

Service Oriented Architecture (SOA) a buzzing word in the world of software development these days. In this article, we will try to understand what the SOA is and how to create and consume it in ASP.NET with C#.

This article talks about 6 ways of doing locking in .NET. It starts with concurrency problems and then discusses about 3 ways of doing optimistic locking. As optimistic locking does not solve the concurrency issues from roots, it introduces pessimistic locking. It then moves ahead to explain how isolation levels can help us implement pessimistic locking. Each isolation level is explained with sample demonstration to make concepts clearer.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/18/2013 8:46:41 PM