Solved

Trying to use a Custom Exception object..

Posted on 2011-03-22
6
267 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

778 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