c#用户可以自己定义异常,自定义异常派生自ApplicationExpection类。
class InputBiggerThanZero : ApplicationException
{
public InputBiggerThanZero(string message) : base(message)
{
}
}
class GetInput
{
int num;
public GetInput()
{
num = 0;
}
public void UserInput(int input)
{
if (input <= 0)
throw (new InputBiggerThanZero("Input is less than zero!"));
else
Console.WriteLine("Input is {0}", input);
}
}
class Program
{
static void Main(string[] args)
{
int input;
input=int.Parse( Console.ReadLine());
GetInput getInput = new GetInput();
try
{
getInput.UserInput(input);
}
catch (InputBiggerThanZero e)
{
Console.WriteLine("{0}",e.Message);
}
}
}