我們有一個圖像上傳頁面,如果用戶的上傳時間超過15分鐘,將會超時。使用HttpException捕獲超時
我們正在捕獲發生超時的HttpException。但是我們怎麼能知道由於超時而發生異常,所以我們可以返回一個特定的消息?
我們的代碼:
try
{
// do stuff here
}
catch (HttpException ex)
{
// What can we check to know if this is a timeout exception?
// if (ex == TimeOutError)
// {
// return "Took too long. Please upload a smaller image.";
// }
return "Error with image. Try again.";
}
catch (Exception ex)
{
return "Error with image. Try again.";
}
什麼超時錯誤的樣子:
System.Web.HttpException (0x80004005): Request timed out.
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.GetMultipartContent()
at System.Web.HttpRequest.FillInFormCollection()
at System.Web.HttpRequest.EnsureForm()
at System.Web.HttpRequest.get_Form()
at MyStore.upload.ProcessRequest(HttpContext context)
ex.ErrorCode=-2147467259
ex.GetHttpCode=500
ex.WebEventCode=0
我猶豫簡單地做一個if
語句的錯誤代碼進行比較的上方。
HttpCode 500
似乎是一個通用的Internal Server Error
代碼,可能發生的不僅僅是一個超時異常。
ErrorCode -2147467259
是我不熟悉的東西。如果該數字在超時錯誤中保持不變,並且永遠不會發生非超時例外情況,那麼我可以對此數字進行if
比較。
我想必須有一個簡單的方法來知道,如果HttpException
是超時異常,ALA是這樣的:
if (ex == TimeoutError) // what should this be?
UPDATE:
我剛纔試圖追趕TimeoutException
,如下所示,但它仍然只能被HttpException
捕獲。
try
{
// do stuff here
}
catch (TimeoutException ex)
{
// Timeout doesn't get caught. Must be a different type of timeout.
// So far, timeout is only caught by HttpException.
return "Took too long. Please upload a smaller image.";
}
catch (HttpException ex)
{
// What can we check to know if this is a timeout exception?
// if (ex == TimeOutError)
// {
// return "Took too long. Please upload a smaller image.";
// }
return "Error with image. Try again.";
}
catch (Exception ex)
{
return "Error with image. Try again.";
}
前的typeof TimeoutException異常 –
@YaWang你應該把那作爲一個答案。 –
你去那裏:D –