Son*_*nül 3 c# error-handling try-catch
每个人都在C#中使用Try-Catch.我知道.
例如;
static void Main()
{
string s = null; // For demonstration purposes.
try
{
ProcessString(s);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
一切都好.
但是如何为特定错误指定名称?
例如;
try
{
enter username;
enter E-Mail;
}
catch
{
}
Run Code Online (Sandbox Code Playgroud)
ErrorMessage- >("用户名已存在")ErrorMessage- >("电子邮件正在使用")我怎么能这样做C#?
最好的祝福,
Soner
if(UserNameAlreadyExists(username))
{
throw new Exception("Username already exists");
}
if(EmailAlreadyExists(email))
{
throw new Exception("Email already exists");
}
Run Code Online (Sandbox Code Playgroud)
这将回答你的问题.
但是异常处理不能用于执行那些检查.例外代价很高,并且适用于无法从错误中恢复的特殊情况.
| 归档时间: |
|
| 查看次数: |
912 次 |
| 最近记录: |