Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP & Databases with PDO PDO Queries & Results Queries with PDO

Error handling - how much is it used in production?

We're getting to our second use of the try/catch construction in our code. Exception handling is something that's not extensively covered in tutorials, and even here at TTH, we don't use it in our example projects. I'm wondering, how often is it used out in the wild of professional web development? How do we learn best practice for using try/catch?

3 Answers

As you get further into the PHP courses you start to see try catch blocks more often. I know that I have been using them extensively in a project I am developing. They also say you should never have an uncaught error. My first thought was why would I have an error if it was working on deployment. But then I realized there may be an issue with the database or the code gets corrupted. It is important to make sure the errors are caught for that reason on deployment.

Another point is that the PHP manual wraps example code in try/catch blocks. I think that is a good indication of the importance of using them.

Anthony Martinez
Anthony Martinez
13,111 Points

Try/catch statements and other implementations of control flow are fairly standard. You may not see them outright when you're interacting with a website or service, but they're a pretty common and arguably foundational aspect to any program. Any backend service almost certainly uses try/catch blocks. Hope that's helpful.