Bird
Raised Fist0
Denoframework~10 mins

Error handling and logging in Deno - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Error handling and logging
Start Program
↓
Try Block: Run Code
↓
Error Occurs?
No→Continue Normal Flow
Yes↓
Catch Block: Handle Error
↓
Log Error Details
↓
Program Ends or Recovers
The program tries to run code inside a try block. If an error happens, it jumps to catch block to handle and log the error, then continues or ends.
Execution Sample
Deno
try {
  const data = JSON.parse('{"name": "Deno"}');
  console.log('Parsed:', data.name);
} catch (error) {
  console.error('Error:', error.message);
}
This code tries to parse JSON and logs the name. If JSON is invalid, it catches and logs the error message.
Execution Table
StepActionEvaluationResult
1Enter try blockJSON.parse('{"name": "Deno"}')Object { name: 'Deno' }
2Log parsed nameconsole.log('Parsed:', data.name)Output: Parsed: Deno
3No error thrownCatch block skippedNo error logged
4Program ends normally--
💡 No error occurred, so catch block was skipped and program ended normally.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dataundefined{ name: 'Deno' }{ name: 'Deno' }{ name: 'Deno' }
errorundefinedundefinedundefinedundefined
Key Moments - 3 Insights
Why does the catch block not run if JSON is valid?
Because no error is thrown during JSON.parse, the program continues normally and skips the catch block as shown in execution_table step 3.
What happens if JSON.parse fails?
If JSON.parse throws an error, execution jumps immediately to the catch block where the error is caught and logged, preventing program crash.
Why do we log error.message instead of the whole error?
Logging error.message shows a clear, readable error description. The full error object may contain extra info not needed for simple logs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'data' after step 1?
Aundefined
Bnull
C{ name: 'Deno' }
DError object
💡 Hint
Check variable_tracker row for 'data' after step 1.
At which step does the program decide to skip the catch block?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
See execution_table step 3 where catch block is skipped.
If the JSON string was invalid, which part of the flow would change?
ACatch block would run and log error
BProgram would skip logging
CTry block would complete without error
DVariable 'data' would hold parsed object
💡 Hint
Refer to concept_flow where error causes jump to catch block.
Concept Snapshot
try {
  // code that might fail
} catch (error) {
  // handle and log error
}

- Code in try runs normally if no error
- If error occurs, catch runs
- Use console.error to log error.message
- Prevents program crash by handling errors
Full Transcript
This visual execution shows how error handling and logging works in Deno using try-catch. The program tries to parse JSON inside the try block. If parsing succeeds, it logs the parsed name and skips the catch block. If parsing fails, it jumps to the catch block where the error is caught and logged using console.error. Variables like 'data' hold the parsed object only if no error occurs. This prevents the program from crashing and helps track errors clearly.

Practice

(1/5)
1. What is the main purpose of using try...catch in Deno?
easy
A. To handle errors and keep the program running smoothly
B. To speed up the program execution
C. To create new files automatically
D. To log messages to the console

Solution

  1. Step 1: Understand the role of try...catch

    The try...catch block is used to catch errors that happen during program execution.
  2. Step 2: Identify the effect on program flow

    By catching errors, the program can handle them gracefully and continue running instead of crashing.
  3. Final Answer:

    To handle errors and keep the program running smoothly -> Option A
  4. Quick Check:

    Error handling = To handle errors and keep the program running smoothly [OK]
Hint: Try...catch blocks catch errors to prevent crashes [OK]
Common Mistakes:
  • Thinking try...catch speeds up code
  • Confusing error handling with logging
  • Believing try...catch creates files
2. Which of the following is the correct syntax to log an error message in Deno?
easy
A. console.log.error('An error occurred')
B. console.error('An error occurred')
C. console.err('An error occurred')
D. console.errorMessage('An error occurred')

Solution

  1. Step 1: Recall the console methods in Deno

    Deno uses standard JavaScript console methods like console.error() to log error messages.
  2. Step 2: Check each option's syntax

    Only console.error('An error occurred') is valid syntax for logging errors.
  3. Final Answer:

    console.error('An error occurred') -> Option B
  4. Quick Check:

    console.error() logs errors = console.error('An error occurred') [OK]
Hint: Use console.error() to log errors clearly [OK]
Common Mistakes:
  • Using non-existent console methods
  • Mixing console.log and error methods incorrectly
  • Misspelling console.error
3. What will be the output of this Deno code?
try {
  throw new Error('Oops!')
} catch (e) {
  console.log('Caught:', e.message)
}
medium
A. Caught: Oops!
B. Error: Oops!
C. Caught: undefined
D. No output

Solution

  1. Step 1: Understand the throw and catch behavior

    The code throws an error with message 'Oops!'. The catch block catches it and accesses e.message.
  2. Step 2: Analyze the console.log output

    The catch block logs 'Caught:' followed by the error message, so output is 'Caught: Oops!'.
  3. Final Answer:

    Caught: Oops! -> Option A
  4. Quick Check:

    Catch logs error message = Caught: Oops! [OK]
Hint: Catch block logs error.message after throw [OK]
Common Mistakes:
  • Expecting full error object printed
  • Confusing error message with undefined
  • Thinking no output occurs
4. Identify the error in this Deno code snippet:
try {
  const data = JSON.parse('{ bad json }')
} catch {
  console.log('Parsing failed', e.message)
}
medium
A. console.log should be console.error
B. JSON.parse syntax is correct
C. Missing error parameter in catch block
D. try block should not have code

Solution

  1. Step 1: Check catch block syntax

    In Deno (and modern JavaScript), catch block can omit error parameter, but to access error details, it must be declared.
  2. Step 2: Identify the problem with missing error parameter

    The code tries to catch error but does not declare error variable, so error details can't be accessed if needed.
  3. Final Answer:

    Missing error parameter in catch block -> Option C
  4. Quick Check:

    Catch needs error param to access error = Missing error parameter in catch block [OK]
Hint: Always include error param in catch to access error info [OK]
Common Mistakes:
  • Assuming catch() always requires error param
  • Thinking console.log is wrong for errors
  • Believing try block can't have code
5. You want to log an error with a timestamp and the error message in Deno. Which code snippet correctly does this?
hard
A. try { throw new Error('Fail') } catch (e) { console.log('Error:', e) }
B. try { throw new Error('Fail') } catch (e) { console.error(e) console.log(new Date()) }
C. try { throw new Error('Fail') } catch { console.error('Error occurred') }
D. try { throw new Error('Fail') } catch (e) { console.error(`[${new Date().toISOString()}] Error: ${e.message}`) }

Solution

  1. Step 1: Understand the requirement for timestamp and message

    The log must include current time in ISO format and the error message.
  2. Step 2: Check each option for correct formatting

    try { throw new Error('Fail') } catch (e) { console.error(`[${new Date().toISOString()}] Error: ${e.message}`) } uses template literals to combine timestamp and error message in one console.error call, which is correct.
  3. Final Answer:

    try { throw new Error('Fail') } catch (e) { console.error(`[${new Date().toISOString()}] Error: ${e.message}`) } -> Option D
  4. Quick Check:

    Timestamp + error message in one log = try { throw new Error('Fail') } catch (e) { console.error(`[${new Date().toISOString()}] Error: ${e.message}`) } [OK]
Hint: Use template literals with new Date().toISOString() for timestamps [OK]
Common Mistakes:
  • Logging error and timestamp separately
  • Missing error parameter in catch
  • Using console.log instead of console.error for errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard