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.
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.
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 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.
Comment