Skip to content

Latest commit

 

History

History
105 lines (73 loc) · 2.84 KB

throw_error.md

File metadata and controls

105 lines (73 loc) · 2.84 KB

Adding Errors in JS

throw new Error('message') in JavaScript works similarly to raising an exception in Python with raise Exception('message').

Simple example, where this checks for an array length and throws and error if it's greater than 3

const names = ['John', 'Paul', 'George', 'Ringo']

try {
  if (names.length > 3) {
    throw new Error('The array is too long')
  }
} catch (error) {
  console.error(error)
}

More detailed insight and Python comparison


🔍 JavaScript (throw new Error)

function validateIntegerId(id) {
  const parsedId = Number(id)

  if (!Number.isInteger(parsedId) || parsedId <= 0) {
    throw new Error('Invalid ID format') // Throws an error
  }

  return parsedId
}

try {
  const id = validateIntegerId('5.1') // Invalid input
  console.log(`Valid ID: ${id}`)
} catch (error) {
  console.error(`Caught error: ${error.message}`)
}

🔹 Output:

Caught error: Invalid ID format
  • If an error is thrown, execution jumps to the catch block.
  • Without a try-catch, it would crash the program.

🐍 Python (raise Exception)

def validate_integer_id(id):
    parsed_id = float(id)  # Convert to number

    if not parsed_id.is_integer() or parsed_id <= 0:
        raise ValueError("Invalid ID format")  # Raise an error

    return int(parsed_id)

try:
    id = validate_integer_id("5.1")  # Invalid input
    print(f"Valid ID: {id}")
except ValueError as e:
    print(f"Caught error: {e}")

🔹 Output:

Caught error: Invalid ID format
  • raise ValueError('Invalid ID format') stops execution like throw new Error().
  • The except block catches it, just like catch in JavaScript.

Key Similarities

✅ Both throw new Error() and raise Exception() stop execution immediately.
✅ Both can be caught with try-catch (JS) or try-except (Python).
✅ Both help handle validation errors cleanly without crashing the program.


Key Differences

Feature JavaScript (throw new Error) Python (raise Exception)
Syntax throw new Error("message") raise Exception("message")
Error Handling try { } catch (error) { } try: except Exception as e:
Custom Errors class MyError extends Error {} class MyError(Exception):
Async Handling try { await someAsync() } catch (error) {} Uses asyncio for async errors

Final Thoughts

Yes, throw new Error() in JavaScript is basically the same as raise Exception() in Python. The main difference is the syntax, but both serve the same purpose: stopping execution and signaling that something went wrong. 🚀