2012-04-05 9 views
0

PHP에 대해 읽고있는 중에 나는 try {}catch {}을 본 사람이 무엇을 의미하는지 궁금합니다.PHP에서 예외가 발생했습니다. 설명해주세요.

+1

http://stackoverflow.com/questions/933081/try-catch-statements-in-php 유용 할 수 있습니다. –

+1

이것은 매우 기본입니다. 프로그래밍의 기본 사항을 이해할 수 있도록 좋은 PHP 책을 골라 읽는 것이 좋습니다. –

답변

2

엔트리 레벨 프라이머를 예외 :

function doSomething($arg1) 
{ 
    if (empty($arg1)) { 
     throw new Exception(sprintf('Invalid $arg1: %s', $arg1)); 
    } 

    // Reached this point? Sweet lets do more stuff 
    .... 
} 

try { 

    doSomething('foo'); 

    // Above function did not throw an exception, I can continue with flow execution 
    echo 'Hello world'; 

} catch (Exception $e) { 
    error_log($e->getMessage()); 
} 

try { 

    doSomething(); 

    // Above function DID throw an exception (did not pass an argument) 
    // so flow execution will skip this 
    echo 'No one will never see me, *sad panda*'; 

} catch (Exception $e) { 
    error_log($e->getMessage()); 
} 

// If you make a call to doSomething outside of a try catch, php will throw a fatal 
// error, halting your entire application 
doSomething(); 

기능을 배치하여/방법은 try/catch 블록 내에서 호출하면 응용 프로그램의 흐름 실행을 제어 할 수 있습니다.

관련 문제