我有一个接受用户登录的网络表单 - 密码、电子邮件。然后我调用一个授权类来测试字段,并在适用时抛出异常。我的问题是:在我的代码隐藏页面中,我应该如何管理/使用这些异常?
授权类 - 电子邮件属性
public string Email
{
get { return _email; }
set
{
if (value == null)
throw new ArgumentNullException("Email", "The email address is null");
if (value == string.Empty)
throw new ArgumentException("Email", "No email has been entered");
if (!IsValidEmail(value))
throw new ArgumentException("Email", "This is an invalid email address");
_email = value;
}
}
代码隐藏页面 - 某种检查
if (auth.Login(txtUsername.Text, txtPassword.Text) == true) //or whatever the invokation might be
// do somehting with exceptions???
此时我不知道我应该如何处理 Authorization 类生成的异常。
最佳答案
您还应该在客户端验证 txtUserName.Text
和 txtPassword.Text
字段不为空。
至于异常,您应该有一个全局处理程序,如果抛出异常(即,如果客户端验证未能捕获某些内容并且服务器抛出异常),它会向用户提供一个很好的错误消息。您不需要在客户端捕获个别异常。
关于c# - 我如何管理另一个类抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4452672/