类赋值是在这种情况下创建自己的自定义异常(MyEmptyStringException)。
这就是我所拥有的:
class Program
{
public class MyEmptyStringException : Exception
{
public MyEmptyStringException(string message) : base(message) { }
}
public class MyOutOfRangeException : Exception
{
public MyOutOfRangeException(string message) : base(message) { }
}
static void Main(string[] args)
{
string testStr;
byte testByte;
bool validValue = false;
Student myStudent = new Student();
do
{//start do loop
Console.WriteLine("Enter student's first name");
try
{
testStr = (Console.ReadLine());
if ((string.IsNullOrEmpty(testStr)))
{
throw new MyEmptyStringException("Answer was blank");
// Console.WriteLine("String is empty");
}
else
{
validValue = true;
} //end if
}
catch (FormatException e)
{
Console.WriteLine("\n" + e.Message + "Format Exception!\n");
}
catch (OverflowException e)
{
Console.WriteLine("\n" + e.Message + "Overflow Exception\n");
}
} while (!validValue);
myStudent.firstName = Console.ReadLine();
对我而言,这里有错误:
throw new MyEmptyStringException("Answer was blank");
错误消息:MyEmptyStringException未处理。任何帮助将不胜感激。
答案 0 :(得分:2)
抓住您的自定义例外:
MyEmptyStringException和MyOutOfRangeException
例如:
try
{
}
catch (MyEmptyStringException mese)
{
Console.WriteLine(mese.Message);
}
catch (MyOutOfRangeException moofre)
{
Console.WriteLine(moofre.Message);
}