2012-06-19 6 views
0

이것은 공상 과학 요청과 같을 것입니다. catch() {} 블록에서 예외가 발생한 try {} 블록의 시작 부분으로 돌아갈 방법이 있습니까?예외에서 돌아 가기

try 
{ 
    // make OAuth request 
} 
catch(OAuthException $e) 
{ 
    // if(){} 
    // If tells me that the Exception was thrown because the access token is expired 
    // I have alternative access token (that's always up to date, but there is a catch why I void using it) 
    // I set it as a new access token and want to repeat the try {} block 
} 

분명히 goto 좀 더 정교한 접근 방식이 있다면하지만, 내가 찾고, 그것을 할 수 있습니다 : 여기

은 예입니다.

+0

당신이 예외를 잡을 때 다시 호출 후 별도의 함수에 넣어 해봤 캐치 섹션에서 같은 함수를 호출 할 수 있습니다? – DRTauli

답변

2

당신은 함수 내부에 코드를 포장하고

function remotePost($accessToken){ 

    try{ 

    }catch(OAuthException $e){ 
    //the one used is not alternative token and if there is an alternative access token 
    return remotePost($alternativeAccessToken); 
    } 
} 
2

while 루프.

do { 
    $ok = false; 
    try { 
    // something 
    $ok = true; 
    } catch (...) { 
    // something 
    } 
} while (!$ok); 

AksharRoop의 및 Broncha의 솔루션은 당신이 (당신이 설명하는 특정 시나리오에 대한 즉) 백업 계획의 제한된 번호가 특히도 좋다. while을 사용하면 다소 일반적입니다.

+0

당신의 답변이 선택됩니다 ;-) – AksharRoop

2

새 토큰으로 다시 호출 할 수 있도록 try 블록을 별도의 함수로 이동하십시오.

try 
{ 
    MakeAuthRequest(token); 
} 
catch(OAuthException $e) 
{ 
    if (failedDueToToken) 
    { 
     MakeAuthRequest(newToken); 
    } 
}