我为我的应用程序不期望的每个条件创建了异常. UserNameNotValidException,PasswordNotCorrectException等等.
但是我被告知我不应该为这些条件创建例外.在我的UML中,那些是主流的例外,为什么它不应该是例外?
创建例外的任何指导或最佳实践?
在这种情况下,您如何纠正处理异常的方法?最初,我希望以下列方式使用trycatch将捕获上载失败时助手类抛出的异常.
我的目标是返回"false"并打开一个Messagebox,并显示错误消息,我将从我的上传尝试中取出HTML响应.我试图这样做,而不是采取不好的做法,并将GUI代码放入我的助手类.
try
{
// returns bool
successful = UploadHelper.Upload(uploadToPath, File.ReadAllBytes(uploadFromPath), properties);
}
catch (Exception ex)
{
string error = ex.Message;
}
Run Code Online (Sandbox Code Playgroud)
助手班:
public static bool Upload(string webUrl, string documentName, byte[] bytes, Dictionary<string, object> metaInfo, out string result)
{
try
{
using (WebClient webClient = new WebClient())
{
//result is HTML string containing data
result = Encoding.UTF8.GetString(webClient.UploadData(webUrl + "/_vti_bin/_vti_aut/author.dll", "POST", data.ToArray()));
// if fails throw exception
if (result.IndexOf("\n<p>message=successfully") < 0)
throw new Exception(result);
}
}
catch (Exception ex)
{
result …Run Code Online (Sandbox Code Playgroud)