Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My custom exception isn't working, please can anyone give me some advice???

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Validator_Click(object sender, RoutedEventArgs e)
        {

            catchingException test = new catchingException();

            try 
            {
                
                
               //Validating Order
                
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Schemas.Add( "OrderValidator.xsd");
                settings.ValidationType = ValidationType.Schema;


                XmlReader order = XmlReader.Create("order.xml", settings);
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(order);

                ValidationEventHandler handler = new ValidationEventHandler(ValidationEventHandler);

                xdoc.Validate(handler);
               // test.catchException();
                    
            }
         
            catch (databasetypeException val)
            {
               
                MessageBox.Show(val.Message);

            }
        }


C#
[Serializable]
   public class databasetypeException : Exception
   {
       public databasetypeException()
           : base()
       {
       }
       public databasetypeException(string message)
           : base(message)
       {
       }
       public databasetypeException(string message, Exception innerException)
           : base(message, innerException)
       {
       }
       public databasetypeException(SerializationInfo info, StreamingContext context)
           : base(info, context)
       {
       }

       public void catchException()
       {
           Exception format = new Exception("Format is invalid ");
           throw format;
       }
   }
   }
Posted
Updated 8-Apr-14 10:29am
v3
Comments
Richard C Bishop 8-Apr-14 16:40pm    
What is not working about it?
David_Wimbley 8-Apr-14 16:43pm    
The reason why its not working is because whatever code being executed in your try block doesn't throw a databasetypeException error.

What would be helpful is a stack trace of what it is you are seeing as my VPN to your desktop is currently out of service. Also, could include whatever code you've got in there that is suppose to throw the databaseTypeException...my guess is the code is probably throwing an ArgumentException or something "generic" and that is why it isn't hitting the try block.
Alexander24 8-Apr-14 17:07pm    
I get an error in the try block: to be specific in xdoc.load(order); what do you mean with doesn't throw a databasetype error
Sergey Alexandrovich Kryukov 8-Apr-14 17:01pm    
"Not working..." is not informative.
—SA
Alexander24 8-Apr-14 17:17pm    
Thank you for your advice, and you right, I have been spending a lot of my time learning C#. If you have any other advice feel free to tell me :)

The code, exception-wise, makes no sense at all, that's why. First of all, you define the exception class databasetypeException, but never throw an instance of it. The method catchException make no sense, it does not catch anything, only throws some exception of unrelated type System.Exception. No wonder: methods catching exceptions make no sense, because you have to use some try-catch blocks anyway. (At the same time, methods handling some exceptions do make sense.) Anyway, you never call this method.

You have only one try-catch block. Most likely, you catch the exception too locally. Usually, exceptions are caught in very few, strategically chosen parts of code; in most cases, you just let them go (propagate) from the current stack frame, far from the stack frame of throwing it. But I cannot blame you for doing it wrong, because I understand you are doing some study, which is good.

I tried to quickly explain this mechanism ("time machine") in my past answers:
Does Exception in C# Constructor Cause Caller Assignment to Fail?[^],
where was stored .net exceptions in operating system[^].

In effect, the idea is to highly isolate "normal" instruction flow from exceptional processing. It helps you to forget about most exceptional cases (including, but not limited to errors) in most of your development cycle.

What else? Traditionally, application-specific exceptions like yours should be derived from System.ApplicationException, not from System.Exception. This is not a big problem, just a matter of neat style of the design of your code, and supportability.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Apr-14 23:24pm    
Exception is a class, so there is no such thing as "call an exception". The try-catch block shown in the body of your question is the correct example of catching the exception.
However, something tells me that you don't quite understand how they work. Not 100% sure though.

Remember, the you use exception classes other than that already available in .NET BCL for some particular purpose: to pass additional information up the stack, to some exception handler (catch block). However, the type information of the exception is already some application-specific information. So, formally your try-catch block is correct, but you should understand all the uses of exception handling...

—SA
Here I am still dealing with my problem, my question is. after I have created my custom exception which includes a custom message, how do call or add that exception to my try-catch block. thank you everyone for your help
namespace ValidationProgram
{
    [Serializable]
    public class MyException : Exception
    {
        public MyException() { }
        public MyException(string message) : base(message) { }
        public MyException(string message, Exception inner) : base(message, inner) { }
        protected MyException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }

}


C#
namespace ValidationProgram
{
    public class testException : MyException
    {
        public override string Message
        {
            get
            {
                return "data type is incorrect";
            }
        }
    }
}
 
Share this answer
 

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