From d67d5a358b81554d6e1498ad3aa3f7906872aae4 Mon Sep 17 00:00:00 2001 From: "David A. Wheeler" Date: Wed, 14 Aug 2024 14:31:20 -0400 Subject: [PATCH] Minor cleanup of lab handling-errors (#580) Signed-off-by: David A. Wheeler --- docs/labs/handling-errors.html | 57 ++++++++++++++-------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/docs/labs/handling-errors.html b/docs/labs/handling-errors.html index 857ae7e7..b2c00814 100644 --- a/docs/labs/handling-errors.html +++ b/docs/labs/handling-errors.html @@ -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); } - - @@ -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* - +
-

Lab Exercise Handling Errors

+

Lab Exercise handling-errors

This is a lab exercise on developing secure software. For more information, see the introduction to @@ -151,31 +149,28 @@

Task Information

To complete this task:

    -
  1. Locate the function in your code that uses return codes to indicate success or failure. In our case, the function is divide.
  2. -
  3. Modify the function to throw an error when an invalid operation is detected. In our case, we throw an error when the parameter b is zero.
  4. -
  5. Set the error message to "Division by zero is not allowed".
  6. -
  7. Update the success path to return the result of the division operation.
  8. -
  9. Modify the calling code to use a try block to wrap the call to the divide function.
  10. -
  11. Within the try block, log the result of the division operation if no error is thrown.
  12. -
  13. Add a catch block to handle any errors that might be thrown by the divide function.
  14. -
  15. For both the success and error paths, log the appropriate message to console or to error.
  16. +
  17. Locate the function in your code that uses return codes to indicate success or failure. In our case, the function is divide.
  18. +
  19. Modify the function to throw an error when an invalid operation is detected. In our case, we throw an error when the parameter b is zero.
  20. +
  21. Set the error message to "Division by zero is not allowed".
  22. +
  23. Update the success path to return the result of the division operation.
  24. +
  25. Modify the calling code to use a try block to wrap the call to the divide function.
  26. +
  27. Within the try block, log the result of the division operation if no error is thrown.
  28. +
  29. Add a catch block to handle any errors that might be thrown by the divide function.
  30. +
  31. For both the success and error paths, log the appropriate message to console or to error.

Interactive Lab ()

Please change the code below so the divide function and the calling code use exception handling instead of a return code mechanism. The divide 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 result, catch any error into a variable named `error`, and display an appropriate message.

-

-
-// Implement a simple division method that returns the result of a / b
+
// Implement a simple division method that returns the result of a / b
 function divide(a, b) {
   if (b === 0) {
     
-      
   } else {
     
@@ -183,18 +178,14 @@ 

Interactive Lab ()

} // This code calls the divide function and logs the result or error - - - -
+