Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to use a Custom Exception object..

Posted on 2011-03-22
6
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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