Skip to Content
LearnCode ReviewQuality AssuranceException Handling

Exception Handling

Let’s look at how to suggest improvements for exception handling in code reviews. We’ll show you a simple example and explain why the feedback matters.

The Scenario

A developer has written code to handle user data. The code works but could handle errors better.

getUserData.ts
// Before function getUserData(userId: string) { try { return db.getUser(userId) } catch (error) { console.log(error) return null } }

PR Comment

Choose the comment that you think is the most constructive and helpful.

// After class UserNotFoundError extends Error { constructor(userId: string) { super(`User with ID ${userId} not found`) this.name = 'UserNotFoundError' } } function getUserData(userId: string) { try { const user = db.getUser(userId) if (!user) { throw new UserNotFoundError(userId) } return user } catch (error) { if (error instanceof UserNotFoundError) { logger.error(`User not found: ${userId}`) } else { logger.error('Database error:', error) } throw error } }

Click here to learn more

Improvements

1. Error Clarity

  • Specific error types for different cases
  • Clear error messages
  • Better-debugging information

2. Error Handling

  • Proper error propagation
  • Appropriate error logging
  • Better error recovery

3. Code Quality

  • More maintainable code
  • Better error tracking
  • Clearer error flow

Tips

1. Start Positive

  • Acknowledge the working code
  • Show you understand the current approach
  • Example: “Great work on the user data retrieval!“

2. Explain the Benefits

  • Why does error handling matter?
  • How does it help?
  • Example: “This helps with debugging”

3. Be Specific

  • Suggest concrete improvements
  • Explain why they matter
  • Example: “Adding specific error types helps with debugging”

4. Keep It Friendly

  • Focus on improvement, not criticism
  • Use encouraging language
  • Example: “Thanks for considering error handling!”

Common Pitfalls to Avoid

1. Being Too Critical

  • ❌ “This error handling is terrible.”
  • ✅ “Good work! Here’s how we can make it even more robust.”

2. Not Explaining Why

  • ❌ “Add proper error handling.”
  • ✅ “Adding specific error types helps with debugging.”

3. Ignoring the Positive

  • ❌ “This needs error handling.”
  • ✅ “Great work! Here’s how we can make it even more maintainable.”

Remember: The goal is to help your teammate write better code while maintaining a positive and collaborative environment!

Last updated on
Review SpeedLogging