- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: php-code-style/undefined-exception
Language: PHP
Severity: Error
Category: Error Prone
This rule is crucial in PHP development as it ensures that the exceptions being caught are actually defined and can be handled appropriately. Undefined exceptions can cause unexpected behavior in your code, making it difficult to debug and maintain.
Catching an undefined exception can lead to a fatal error that halts the execution of your script. This can be particularly problematic in a production environment, where it can lead to a poor user experience or even data loss.
To adhere to this rule, make sure that any exception you are trying to catch is defined in your code or within the PHP core exceptions. Using the use
keyword at the start of your scripts to import exceptions from other namespaces is a good practice. Always remember to catch the most specific exceptions first and then the more generic ones. This way, you can handle specific errors in a custom way and have a fallback for any unexpected exception.
For instance, instead of using catch (Exception $ex)
, use the specific exception that your code may throw like catch (SpecificException $se)
. This way, you ensure that your code is ready to handle the specific exceptions it may encounter during execution.
<?php
try {
willThrow();
} catch (Exception $ex) {
echo $ex->message;
}
<?php
use Foo\SpecificException;
try {
willThrow();
} catch (\Exception $ex) {
echo $ex->message;
}
try {
willThrow();
} catch (SpecificException $se) {
echo $se->message;
}
<?php
use Exception;
try {
willThrow();
} catch (Exception $ex) {
echo $ex->message;
}
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products