[英]c# custom exceptions is unhandled
類分配是在這種情況下創建自己的自定義異常(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未處理。 任何幫助,將不勝感激。
抓住您的自定義例外:
MyEmptyStringException和MyOutOfRangeException
例如:
try
{
}
catch (MyEmptyStringException mese)
{
Console.WriteLine(mese.Message);
}
catch (MyOutOfRangeException moofre)
{
Console.WriteLine(moofre.Message);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.