REPONSE.REDIRECT가있는 중첩 Try-Catch
문제
나는 시도 캐치 블록을 중첩시켰다
try
{
// statements
try
{
// statements
}
catch(SqlException sqlex)
{
Response.Redirect(@"~/Error.aspx?err=" + Server.UrlEncode(sqlex.Message)
+ "&src=" + Server.UrlEncode(sqlex.ToString()));
}
catch (Exception ex)
{
Response.Redirect(@"~/Error.aspx?err=" + Server.UrlEncode(ex.Message)
+ "&src=" + Server.UrlEncode(ex.ToString()));
}
finally
{
conn.Close();
}
}
catch (Exception ex)
{
Response.Redirect(@"~/Error.aspx?err=" + Server.UrlEncode(ex.Message)
+ "&src=" + Server.UrlEncode(ex.StackTrace));
}
finally
{
conn.Close();
}
내부 첫 번째 캐치 블록에서 sqlexeceception이 잡히면, 시도하기 때문에 ... 캐치 서라운드 Response.Redirect
, 전송에 의해 생성 된 나사성 덱스크는 외부 캐치 블록에 의해 잡힌다.
이 문제를 어떻게 해결할 수 있습니까? 감사!
해결책
읽다 이 기사
당신이 가장 할 수있는 일은 시도/캐치 하네스 내부에서 리디렉션하지 않습니다.
string url ;
try
{
// statements
try
{
}
catch(SqlException sqlex)
{
url = @"~/Error.aspx?err=" + Server.UrlEncode(sqlex.Message) + "&src=" + Server.UrlEncode(sqlex.ToString());
}
catch (Exception ex)
{
url = @"~/Error.aspx?err=" + Server.UrlEncode(ex.Message) + "&src=" + Server.UrlEncode(ex.ToString());
}
finally
{
conn.Close();
}
}
catch (Exception ex)
{
url = @"~/Error.aspx?err=" + Server.UrlEncode(ex.Message) + "&src=" + Server.UrlEncode(ex.StackTrace);
}
finally
{
conn.Close();
}
}
if ( url != "" ) {
Response.Redirect(url);
}
else {
// Page logic can continue
}
다른 팁
첫 번째 응답에서 코드 변경을 고려하십시오
if ( url != "" ) { Response.Redirect(url);}
에게
if (!string.IsNullOrEmpty(url)) { Response.Redirect(url);}
내부 시도에서 오류가 발생한시기를 추적하기 위해 변수를 사용하여 ... 캐치 한 다음 응답을 둘러싸고 두 번째로 확인하십시오. if 문으로 저장하십시오.
Try-Catch 블록 내부의 Resdirect를 사용할 때마다 두 번째 매개 변수를 추가해야합니다.
대신 :
Response.Redirect("somepage.aspx");
이 작업을 수행:
Response.Redirect("somepage.aspx", false);
해당 부울이 사실 일 때, 페이지 내용은 새 페이지로 전송됩니다 (양식 값을 사용할 수 있음). 부울이 거짓 인 경우 페이지 내용은 전송되지 않습니다. 이것은 아마도 당신이 원하는 것일 것입니다. 당신은 당신이 진실이 필요한지 알 것입니다.
이것에 대한 또 다른 접근법은 빈 캐치 블록에서 ThreadabortException을 잡는 것입니다.
제휴하지 않습니다 StackOverflow