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)
}
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.
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 likethrow new Error()
.- The
except
block catches it, just likecatch
in JavaScript.
✅ 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.
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 |
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. 🚀