问题描述
我正在尝试编写aspx登录系统,并且登录过程需要对用户名和密码进行
有效性检查。
i被告知提出异常是昂贵的,但我想要一个自定义错误
消息显示取决于发生了什么错误(让我们说:无效
用户名,密码无效,密码必须包含5个或更多字符,
等)。
目前,我的登录方法返回true / false取决于是否
登录成功与否。但是,如果登录失败,我的
调用函数将如何知道发生了什么错误而没有引发异常?
i知道我可以从登录方法返回数字回到调用
函数(其中每个数字代表一个不同的错误),但后来我会
必须检查每个使用
登录方法,并在每次调用
函数时写下appropritate错误消息。
有没有办法可以写我的代码所以我只需要在
登录函数中写错误,如果
登录失败,调用函数会收到错误信息吗?
感谢
i am trying to write aspx login system and the login process requires a
validity check of username and password.
i have been told that raising exception is costly, but i want a custom error
message to be displayed depending on what error occurred (let''s say: invalid
username, invalid password, password must contain 5 or more characters,
etc).
at the moment, my login method is returning true/false depending on whether
the login was succesful or not. however, if the login fails, how will my
calling function know what error occurred without raising an exception?
i know i could return numbers from the login method back to the calling
function (where each number represents a different error), but then i would
have to check the value of the number on every calling function that uses
the login method, and write the appropritate error messages on EVERY calling
function.
is there a way i can write my code so i only have to write the error in the
login function, and calling functions will pick up the error message if the
login fails?
thanks
推荐答案
错误
无效
是否
调用
您可以返回使用枚举值功能。这将保持
非常安全的东西,你可以很容易地处理返回值。
怎么样?
错误不一定是例外。如果您能够处理并预期错误,那么抛出异常通常不是一个好主意。投掷
例外会自动强制你的API用户围绕每个方法包裹一个
try / catch / finally块。
还是我错了? :-)
-
venlig hilsen /关心
anders borum
-
You could return use an enumeration value from the function. This would keep
things pretty typesafe and you could handle the return value pretty easily.
How about that?
An error is not necessarily an exception. If you can handle and anticipate
the error, it''s generally not a good idea to throw an exception. Throwing
exceptions automatically forces the users of your API to wrap a
try/catch/finally block around each method.
Or am I wrong? :-)
--
venlig hilsen / with regards
anders borum
--
这篇关于c#/ aspx错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!