Solved

Trying to use a Custom Exception object..

Posted on 2011-03-22
6
265 Views
Last Modified: 2012-05-11
Hi Guys =)

I'm having problems trying to implement a custom exception object.  The code below, which generates a 'divide by zero' error does not get passed to the custom exception object, instead will i'm stepping through the code it will just stop on the divide by zero line of code and simple display my error in a Visual Studio error box, never goes into the Catch..

If I simply replace the two instances of "catch (CustomeException ex)"  with "catch (Exception ex)" then the catch will pass the error into the default exception object.

Any suggestions?

Thanks in advance =)


-------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ErrorTrappingTest
{
    public class MyFunctions
    {
         
            public bool Method1()
            {
                try
                {
                    bool result = Method2();
                }
                catch (CustomException ex)
                {
                    throw;
                }
                return true;            
            }


            public bool Method2()
            {
                try
                {
                    int a = 5, b = 0;
                    int x = a / b;   // Causes a divide by zero error
                }
                catch (CustomException ex)
                {
                    throw;
                }

                return true;        
            }


             public class CustomException : System.Exception
            {
                public CustomException() : base() { }
                public CustomException(string message) : base(message) { }
                public CustomException(string message, System.Exception inner) : base(message, inner) { }

            }


       
    }
}
0
Comment
Question by:aamado
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35190135
Hi,
You can use as below if are planning to implement Custom Exception to use all over ur application. But pain here is you need to know and catch all system exceptions and catch them and throw the custom exception with more info on the exception scenario.
Here is an modified example:
  public bool Method2()
            {
                try
                {
                    int a = 5, b = 0;
                    int x = a / b;   // Causes a divide by zero error
                }
                catch (DivideByZeroException ex)
                {
                    throw new CustomException("Divide by Zero error occured");
                }

                return true;        
            }
Note: It is better it check for this exception occuring variable check before performing like:
 int x;
if(b> 0)
{
 x = a / b;
}
else
{
throw new CustomException("Invalid value for b is provided");
}
0
 
LVL 11

Expert Comment

by:Sudhakar Pulivarthi
ID: 35190160
Here is more info on exception handling:
http://msdn.microsoft.com/en-us/library/ms173160.aspx
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35190271
You cannot use a custom exception to arbitarily catch other exceptions. Simply adding
catch (CustomException ex)
{
}

Open in new window

Will not automatically convert the DivideByZeroException into your custom type. You still need to catch the DivideByZeroException, and then throw your custom exception instead, if that is what you choose to do.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:aamado
ID: 35190769
Thanks Sudhakar and Carl =)

Carl, I think I get what you're saying..  I was hoping I could just inherit from Exception and thus allow my custom exception to grab any exception type the way the base exception class can.  

My ultimate goal was to extend the base Exception class so that I could add other properties to it.  I think i'm taking the wrong approach..
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 35190800
You can still do that. But if you want to handle existing exception types then you need to catch the more generic Exception and then create and throw your custom exception.
0
 

Author Comment

by:aamado
ID: 35190961
Got it!   Your explanation made it click for me finally!

I modified it this way (and added the properties 'Message' and ''Note" to my custom exception class)  and it is working.

Thanks for your help!

-Alan

--------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ErrorTrappingTest
{
    public class MyFunctions
    {
       
            public bool Method1()
            {
                try
                {
                    bool result = Method2();
                }
                catch (CustomException ex)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw;
                }
           
                return true;            
            }


            public bool Method2()
            {
                try
                {
                    int a = 5, b = 0;
                    int x = a / b; // Causes error
                }
                catch (Exception ex)
                {
                    CustomException customException = new CustomException();
                    customException.Message = ex.Message;
                    customException.Note = "test";

                    throw customException;
                }

                return true;        
            }


             public class CustomException : System.Exception
            {
                public CustomException() : base() { }
                public CustomException(string message) : base(message) { }
                public CustomException(string message, System.Exception inner) : base(message, inner) { }

                public string Message;
                public string Note;


            }


       
    }
}
 
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now