【发布时间】:2016-05-13 22:20:40
【问题描述】:
这太令人困惑了... 我有一个带有 set 和 get 方法的类,这里有几个:
public string Naziv
{
get { return naziv; }
set
{
naziv = value;
if (naziv == "")
{
throw new Exception("Morate uneti naziv radnog mesta.");
}
else if (naziv.Length < 5)
{
throw new Exception("Naziv mora biti duzi od 5 karaktera.");
}
}
}
这个很完美。但是这个:
public string RadnoVreme1
{
get { return radnovreme1; }
set
{
radnovreme1 = value;
if (IsValid(radnovreme1) == false)
{
//Console.WriteLine("1:FALSE ");
throw new Exception("Radno vreme mora biti u formatu '12:00h-20:00h'.");
}
}
}
static bool IsValid(string value)
{
return Regex.IsMatch(value, @"^\d{2}:\d{2}h-\d{2}:\d{2}h");
}
因错误而中断我的解决方案:
“System.Exception”类型的未处理异常发生在 Evidencija.exe 附加信息:Radno vreme mora biti u formatu '12:00h-20:00h'。
另一件事。当取消注释 'Console.WriteLine' 行并注释掉 'throw new Exception' 时,我得到以下输出:
1:FALSE
1:FALSE
1:FALSE
The thread 0x1748 has exited with code 0 (0x0).
为什么要运行 3 次?是因为我的数据库中已经有 3 个存储对象吗?这不应该只在创建新对象时运行吗?
【问题讨论】:
-
好像你在某处设置了 RadnoVreme1 4 次。是否尝试过使用断点单步执行您的代码?
标签: c# class object exception unhandled