Best method in PHP for the Error Handling ? Convert all PHP errors (warnings notices etc) to exceptions?

What is the best method in PHP for the Error Handling ?

is there a way in PHP to Convert all PHP errors (warnings notices etc) to exceptions ?

what the best way/practise to error handling ?

again:

if we overuse exceptions (i.e. try/catch) in many situations, i think application will be halted unnecessary.

for a simple error checking we can use return false; but it may be cluttering the coding with many if else conditions.

what do you guys suggest ?

1

If your development and production environments are separate, you should have the display_errors ini setting set to off on production, and on (and I would set the error_reporting level to -1: all errors) on development. It shouldn’t be too difficult to write code that doesn’t emit any warnings or notices through normal usage unless you’re using a PEAR library, as those have a lot of legacy code that causes warnings.

As for errors and exceptions, on development you should just treat them normally so you can actually see what’s wrong, and on production you can use the set_error_handler and set_exception_handler methods to catch errors and exceptions and display an error page to the user instead and write the actual error to a log file that you can check later.

Other than that, I wouldn’t use any special tactics for error suppression.

When you develop your code you should have all the error reportings in PHP enabled, including notices and warnings. Your production code should never produce a PHP Error. In some special cases you may survive with some PHP Warnings and Notices, but on your production server you should only show the Errors to the user.

Next, PHP Errors mean that you have something really messed up in your code and such things should never reach production. Any amount of decent automated tests will make sure this never happens. => Correction for clarity: “… will make sure this happens rarely.”

I don’t recommend you to transform PHP Errors, Warnings and Notices into Exceptions or “FALSE”. Never! If they happen on your computer the application has a serious problem, so don’t transform the error into something that you can catch and than forget / ignore easily.

When you have situations where your code can go wrong, use proper verification and throw exceptions. Even more, whenever possible make your own exception and throw the one most appropriate for that situation.

For example, instead of letting PHP throw an “Invalid index” when you try to read an element of an array, you may do something like:

if (!isset($superWeapons['nuclear'])) 
  throw new MissingSuperweaponException('We heave no nuclear weapons awailable!');

Than, somewhere at a higher level of your application you can catch all these exceptions and show the user the nice messages you want.

Please note, that exceptions are for exceptional cases, for situations in which your application should never be. Use then only when needed.

When there is a result of an operation, a result which can be TRUE or FALSE or a value or a string, that is not an exceptional case, that is a result or a return value. For example, when you do a search for a string the result can be the index of the searched text. However, not finding the text is not an exceptional case, you should just return zero or false, not an exception.

4

Short Answer: If you use PHP 5, you can handle error with exceptions:

https://www.php.net/manual/en/class.exception.php

This way is cleaner than manual set exception message, because you have access to a try catch system and you can isolate exception handling

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật