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

Scoping Problem Demonstration

  • Initial code fails: "number is not defined" error
  • Variable num defined inside try block doesn't exist outside
  • Only solutions:
    • Move all code inside try block (described as "dumb")
    • Pre-declare with let num = -1 outside try block
  • let num inside try block doesn't work
  • var works 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 = 1 line
    • Variable never gets defined due to early error
    • Accessing undefined variable after catch would cause issues

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

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome