AI summary by TwinMind
The Actual Dumbest Thing About Try/Catch
Theo - t3․gg
11m 47s
JavaScript Error Handling Critique
- JavaScript error handling "kind of sucks" - try-catch is "a disaster"
- Focus on scoping issues in try blocks - underexplored problem
- Inspired by Leah's tweet about try block scoping behavior
Leah's Position on Try Block Scoping
- Try blocks should not behave like regular blocks for scoping variables
- Scoping rarely useful since not conceptually meaningful
- More like code annotation than control flow
- Unlike control flow blocks, no program state only makes sense within try block
- Catch block is actual control flow where scoping makes complete sense
Code Demonstration Setup
- Created basic node template with function
doSomeWork() - Built
getNumber()function:const number = Math.random() * 100- Throws error "number too big" if num > 50
- Returns num otherwise
- Created
randomNumberDoubler()function:- Calls
getNumber()to get num - Returns
num * 2
- Calls
Scoping Problem Demonstration
- Initial code fails: "number is not defined" error
- Variable
numdefined inside try block doesn't exist outside - Only solutions:
- Move all code inside try block (described as "dumb")
- Pre-declare with
let num = -1outside try block
let numinside try block doesn't workvarworks because it hoists (described as "so dumb")
Block Scoping Logic in JavaScript
- Variables in blocks (if statements) don't exist outside unless using
var - Example with
doubleIfOdd()function demonstrates why scoping exists - If condition might not be hit, variables might not be defined
- Makes sense for conditional blocks where code execution uncertain
Try Block Scoping Analysis
- Try block always gets hit unlike conditional blocks
- Initial confusion about why try block variables get destroyed
- Edge case: error could occur before variable definition
- Example: throwing error before
var num = 1line - Variable never gets defined due to early error
- Accessing undefined variable after catch would cause issues
- Example: throwing error before
Realization and Mind Change
- Understanding emerged: each line in try block conditionally executed
- Condition: "if an error was raised"
- Any given line might not be hit due to earlier error
- TypeScript could theoretically be smarter:
- If return in catch, know values exist
- If no return in catch, assume variables could be undefined
Comparison with Other Languages
- Ethan Nater (effect TS creator) mentioned Zig's approach
- Zig has try keyword and catch cases
- Different error handling paradigm than JavaScript
Final Conclusion
- Changed opinion during video creation process
- Initially wanted Leah's position to be popular
- Now understands technical reasoning behind current behavior
- Still hates developer experience (DX)
- Acknowledges scoping prevents accessing potentially undefined variables
- Conclusion: "JavaScript sucks" regardless of understanding the logic
