Clean Code: Error Handling
Use exceptions for error handling and avoid returning error codes.
Let's explore some examples in TypeScript to help you grasp the concept:
// Bad
function divide(a: number, b: number): number | string {
if (b === 0) {
return 'Error: Division by zero';
}
return a / b;
}
// Good
function divide(a: number, b: number): number {
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
}