Skip to content

Commit

Permalink
Minor cleanup of lab handling-errors (#580)
Browse files Browse the repository at this point in the history
Signed-off-by: David A. Wheeler <dwheeler@dwheeler.com>
  • Loading branch information
david-a-wheeler authored Aug 14, 2024
1 parent 0acd532 commit d67d5a3
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions docs/labs/handling-errors.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
try {
const result = divide(10, 2);
console.log("Result:", result);
} catch (error) {
console.error("Error:", error.message);
} catch (err) {
console.error("Error:", err.message);
}


</script>

<!-- Full pattern of correct answer -->
Expand All @@ -43,10 +41,10 @@
\s* try \{
const result = divide \( 10 , 2 \) ;
console \. log \( ("Result:" | 'Result:' | `Result:`) , result \) ;
\} catch \( error \) {
console.error \( ("Error:" | 'Error:' | `Error:`) , error \. message \) ;
\} catch \( err \) {
console.error \( ("Error:" | 'Error:' | `Error:`) , err \. message \) ;
\} \s*
</script>
</script>

<script id="info" type="application/yaml">
---
Expand All @@ -68,7 +66,7 @@
- - " throw new ERROR(\"Division by zero is not allowed\");"
- index: 0
absent: "throw"
text: Try using the throw keyword to raise an exception, E.g., throw new Error(...).
text: Try using the throw keyword to raise an exception, E.g., throw new Error("Message").
examples:
- - " return { success: false, message: \"Division by zero is not allowed\" };"
- - " return \"Division by zero is not allowed\" ;"
Expand Down Expand Up @@ -105,21 +103,21 @@
- - " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); }"
- index: 2
absent: '\s* catch \s* \( .*? \) { \s* '
text: Catch an error object within the catch block.
text: Use 'catch (...) {...}' to catch an error object within the catch block.
examples:
- - " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch {}"
- index: 2
absent: '\s* console.error \(\s* "Error:" \s* , .*?.message \) '
text: The error is an object with a message property.
absent: '\s* catch \s* \( err \) { \s* '
text: Catch an object named err within the catch block.
examples:
- - " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (error) { console.error( \"Error:\", result); } "
- - " try { const result = divide(10 ,2); console.log ( \"Result:\", result ); } catch (foo) {"
# debug: true
</script>
</head>
<body>
<!-- For GitHub Pages formatting: -->
<div class="container-lg px-3 my-5 markdown-body">
<h1>Lab Exercise Handling Errors</h1>
<h1>Lab Exercise handling-errors</h1>
<p>
This is a lab exercise on developing secure software.
For more information, see the <a href="introduction.html" target="_blank">introduction to
Expand Down Expand Up @@ -151,50 +149,43 @@ <h2>Task Information</h2>
To complete this task:
</p>
<ol>
<li> Locate the function in your code that uses return codes to indicate success or failure. In our case, the function is <tt>divide</tt>.</li>
<li> Modify the function to throw an error when an invalid operation is detected. In our case, we throw an error when the parameter <tt>b</tt> is zero.</li>
<li> Set the error message to "Division by zero is not allowed".</li>
<li> Update the success path to return the result of the division operation.</li>
<li> Modify the calling code to use a try block to wrap the call to the <tt>divide</tt> function.</li>
<li> Within the try block, log the result of the division operation if no error is thrown.</li>
<li> Add a catch block to handle any errors that might be thrown by the <tt>divide</tt> function.</li>
<li> For both the success and error paths, log the appropriate message to console or to error.</li>
<li>Locate the function in your code that uses return codes to indicate success or failure. In our case, the function is <tt>divide</tt>.</li>
<li>Modify the function to throw an error when an invalid operation is detected. In our case, we throw an error when the parameter <tt>b</tt> is zero.</li>
<li>Set the error message to "Division by zero is not allowed".</li>
<li>Update the success path to return the result of the division operation.</li>
<li>Modify the calling code to use a try block to wrap the call to the <tt>divide</tt> function.</li>
<li>Within the try block, log the result of the division operation if no error is thrown.</li>
<li>Add a catch block to handle any errors that might be thrown by the <tt>divide</tt> function.</li>
<li>For both the success and error paths, log the appropriate message to console or to error.</li>
</ol>
<p>
<h2>Interactive Lab (<span id="grade"></span>)</h2>
<p>
Please change the code below so the <tt>divide</tt> function and the calling code use exception handling instead of a return code mechanism.
The <tt>divide</tt> function should throw an error when division by zero is attempted.
The calling code should use a try...catch block to handle the error, catch any error into a variable named `error`, and display an appropriate message.
The calling code should use a try...catch block to handle the error, stgore the result in a constant named <tt>result</tt>, catch any error into a variable named `error`, and display an appropriate message.

<form id="lab">
<pre><code>

// Implement a simple division method that returns the result of a / b
<pre><code>// Implement a simple division method that returns the result of a / b
function divide(a, b) {
if (b === 0) {
<input id="attempt0" type="text" size="65" spellcheck="false"
value='return { success: false, message: "Division by zero is not allowed" };'>

} else {
<input id="attempt1" type="text" size="65" spellcheck="false"
value='return { success: true, result: a / b };'>
}
}

// This code calls the divide function and logs the result or error
<textarea id="attempt2" rows="10" cols="60" spellcheck="false">
<textarea id="attempt2" rows="8" cols="60" spellcheck="false">
const result = divide(10, 2);
if (result.success) {
console.log("Result:", result.result);
} else {
console.error("Error:", result.message);
console.error("Error:", result.message);
}

</textarea>


</code></pre>
</textarea></code></pre>
<button type="button" class="hintButton">Hint</button>
<button type="button" class="resetButton">Reset</button>
<button type="button" class="giveUpButton">Give up</button>
Expand Down

0 comments on commit d67d5a3

Please sign in to comment.