Try Catch with Inheritance problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzz13
    New Member
    • Jun 2010
    • 110

    #1

    Try Catch with Inheritance problem.

    I am have some code where I pass in a value for an employees salary as a negative. Using try catch it tries passing this negative value into my emp.salary where there is a test statement saying if salary is less then 0 then throw a NegativeSalaryE xception.
    Code:
    using System;
    public class DebugNine03
    {
       public static void Main()
       {
          Employee emp = new Employee();
    
          try
          {
             emp.IdNum = 234;
             emp.Salary = -12;
          }
          catch (NegativeSalaryException)
          {
             Console.WriteLine(emp.);
             Console.WriteLine(Environment.StackTrace);
          }
       }
    }
    public class NegativeSalaryException : Exception
    {
       private static string msg = "Employee salary is negative.";
       public NegativeSalaryException() : base()
       {
       }
    }
    
    public class Employee
    {
       public int idNum;
       public double salary;
       public int IdNum
       {
          get
          {
             return idNum;
          }
           set
           {
               idNum = value;
           }
       }
       public double Salary
       {
          get
          {
             return salary;
          }
          set
          {
            if(value < 0)
            {
               NegativeSalaryException salExcep = new NegativeSalaryException();
               throw(salExcep);
            }
            salary = value;
          }
       }
    
    }
    I know that because the salary passed is negative always it is supposed to always have the negativesalary exception occur but I don't understand what the throw statement is doing?

    I am also having issues understanding how to use the value "msg" declared in my NegativeSalaryE xception inside my Main method.

    If anyone has any thoughts on it I would be greatly appreciative. Thanks.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    You're close but missing a few things. I'd highly recommend you read the following, there's some good information there.

    http://msdn.microsoft.com/en-us/libr...8VS.80%29.aspx

    In your case, there's a few key things wrong with your code.

    1) You've got some text in your exception that you're calling the message, but you're not actually doing anything with it. I'm guessing you want this to be the exception message text when no other text is specified? In this case, you can just pass that message along to the appropriate base constructor...

    Code:
    public NegativeSalaryException()
      : base (msg)
    {
    }
    You may also want to consider exposing the constructor that takes a string as a parameter, so users of the exception can specify their own message.

    Code:
    public NegativeSalaryException(string message)
      : base (message)
    {
    }
    2) When you throw your exception, you aren't providing a message. This kind of ties in with the above... but the general method of throwing an exception is to provide some text with it. For example, if I were throwing a generic exception I'd do something like this...

    Code:
    public static float Divide(float numerator, float denominator)
    {
      if (denominator == 0)
        throw new Exception("Cannot divide by zero!");
    
      return numerator / denominator;
    }
    We provide this so the user gets some feedback as to what went wrong. To correct that, either do as mentioned in the above, or provide an exception message for your exception at the time it is thrown.

    3) Last one, and this is more of a suggestion than a requirement. When you catch an exception, I personally find it helpful to provide an exception parameter so you can access information about the exception. With what you have, you're just catching the exception type, but the actual exception is lost. You can access this simply by providing a variable name for the exception...

    Code:
    try
    {
      // some code
    }
    catch (Exception e)
    {
      Console.WriteLine("An exception occurred!");
      Console.WriteLine(e.Message);
      Console.WriteLine(e.StackTrace);
    }
    Hopefully that helps. As always, I'd recommend reading the MSDN doc on this or checking out a few tutorials on exception handling. They provide much more comprehensive information than I can :) Good luck!

    Comment

    • Fuzz13
      New Member
      • Jun 2010
      • 110

      #3
      GaryTextmo You are just a king among men. You help me on so many of my projects so a personal thanks for that. I tried passing the msg string back thru the base
      Code:
         public NegativeSalaryException() : base(msg)
      But how do I use that in my main method now. Or just get it to show that string known as "msg" when the exception occurs? I tried calling it in my console.writeli ne statement but it can't find it?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Read the MSDN for an Exception class:
        http://msdn.microsoft.com/en-us/libr...exception.aspx

        Take note of the constructor section, specifically:
        http://msdn.microsoft.com/en-us/library/48ca3hhw.aspx

        Under remarks: This constructor initializes the Message property of the new instance using the message parameter.

        You should now be able to access the message in your catch statement using the Message property.

        That particular bit of code you have in your post is just a means of setting a default message when one isn't provided, which is what I gathered you wanted to do from the code you wrote in your original post. As I noted in my second code block, you can also create a constructor to pass a string parameter instead of a constant default message if desired.

        Using the code we've created thus far...
        Code:
        // Create an exception with the default message
        NegativeSalaryException nse1 = new NegativeSalaryException();
        
        // Create an exception with a specific message
        NegativeSalaryException nse2 = new NegativeSalaryException("Something bad has happened and I'm pretty sure it's a negative salary, but hey, who knows, right!?");
        Were you to throw each of those, wrapped in a try/catch block, you could output the Message property to see what it was.

        Comment

        Working...