From 0e1af9bb9de22b67e9584b6b97b30cef9d6d2cc0 Mon Sep 17 00:00:00 2001 From: TheMaja Date: Thu, 28 Sep 2023 16:09:07 +0200 Subject: [PATCH 1/4] created new directory for javascript and python error handling --- .../error-handling-assignment/_index.md | 28 +++++ .../javascript-error-handling/_index.md | 2 +- .../python-error-handling/_index.md | 102 ++++++++++++++++++ content/syllabuses/data-eng-part-1.md | 3 +- content/syllabuses/data-sci-part-1.md | 2 + content/syllabuses/web-dev-part-1.md | 3 +- 6 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 content/error-handling/error-handling-assignment/_index.md rename content/{topics => error-handling}/javascript-error-handling/_index.md (98%) create mode 100644 content/error-handling/python-error-handling/_index.md diff --git a/content/error-handling/error-handling-assignment/_index.md b/content/error-handling/error-handling-assignment/_index.md new file mode 100644 index 000000000..63f752271 --- /dev/null +++ b/content/error-handling/error-handling-assignment/_index.md @@ -0,0 +1,28 @@ +--- +content_type: project +flavours: + - java + - javascript + - markdown + - python +prerequisites: + hard: + - error-handling/javascript-error-handling + - error-handling/python-error-handling +protect_main_branch: false +ready: true +submission_type: repo +title: Error Handling Assignment +--- + +Please answer the following questions in markdown files and submit following the instructions below. + +## Questions + +1. In your programming language, what is the fundamental purpose of error handling, and why is it crucial in software development? + +2. In your programming language, what are some poor practices in error handling that developers should avoid? Provide examples if possible. + +3. In your programming language, when should developers catch and handle errors, and what factors should influence this decision? + +4. In your programming language, demonstrate how to raise a custom error with a specific error message. Write a code snippet that showcases this, and explain why it's essential to include clear error messages in custom exceptions. diff --git a/content/topics/javascript-error-handling/_index.md b/content/error-handling/javascript-error-handling/_index.md similarity index 98% rename from content/topics/javascript-error-handling/_index.md rename to content/error-handling/javascript-error-handling/_index.md index 745f25b82..94fedaea6 100644 --- a/content/topics/javascript-error-handling/_index.md +++ b/content/error-handling/javascript-error-handling/_index.md @@ -114,4 +114,4 @@ throw new Error("error message"); - [JavaScript.info error handling](https://javascript.info/error-handling) - how the try catch works and some advice on handling errors - [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) - learn more about try catch statements -- [Stack Overflow - best practices for JavaScript error handling](https://stackoverflow.com/questions/6484528/what-are-the-best-practices-for-javascript-error-handling) \ No newline at end of file +- [Stack Overflow - best practices for JavaScript error handling](https://stackoverflow.com/questions/6484528/what-are-the-best-practices-for-javascript-error-handling) diff --git a/content/error-handling/python-error-handling/_index.md b/content/error-handling/python-error-handling/_index.md new file mode 100644 index 000000000..96813e45b --- /dev/null +++ b/content/error-handling/python-error-handling/_index.md @@ -0,0 +1,102 @@ +--- +content_type: topic +flavours: +- python +ready: true +tags: +- python +title: Python Error Handling Best Practices +--- + +## Exceptions are Valuable + +In Python, exceptions are like alarms that go off when something goes wrong in your code. They're not something you should ignore or just write in a log; you need to deal with them properly. + +When your program runs into a problem, it can cause your program to stop unexpectedly. Python has a way to deal with these problems using try and except blocks. These blocks help you handle errors in a way that doesn't crash your program and allows you to get information about what went wrong. + +## Poor Error Handling Practices + +Let's explore some common mistakes in error handling: + +- Just printing errors + +```python +try: + # Code... +except Exception as error: + print(error) +``` + +- Just printing errors with a different colour + +```python +try: + # Code... +except Exception as error: + print(f"Error: {error}") +``` + +- Returning errors + +```python +try: + # Code... +except Exception as error: + return error +``` + +- Catching expected exceptions only to re-raise them without adding context + +```python +try: + # Code... +except ValueError as error: + raise error +``` + +- Catching and ignoring the exceptions + +```python +try: + # Code... +except Exception as error: + raise Exception("A new error that may or may not be related to the caught error") +``` + +## When to Catch Errors + +It's important to understand when to catch errors. You should only catch errors that you can handle. If you can't handle the error, you should let it bubble up to the caller. This is especially important when you're writing a library or a module that will be used by other developers. If you catch an error that you can't handle, you're essentially hiding the error from the caller. This can lead to unexpected behaviour and bugs. + +## Creating Custom Exceptions + +In Python, you can create custom exceptions when you precisely understand what went wrong or when you want to provide more context to an existing exception. + +There are different ways to raise custom exceptions: + +Just raising a caught exception or any object as an exception + +```python +# AVOID - It's not clear where the exception occurred; you can raise almost anything as an exception +raise error + +raise "Some error" + +raise {} +``` + +Using the Exception class: + +```python +# PREFERRED - It indicates the location of the exception, and the constructor accepts a message for clarity +raise Exception(error) + +raise Exception("Error message") +``` + +## Additional Resources + +[Python Official Documentation on Exceptions](https://docs.python.org/3/tutorial/errors.html) - Learn more about handling exceptions in Python. + +[Real Python - Python Exceptions: An Introduction](https://realpython.com/python-exceptions/) - Comprehensive guide to Python exceptions and error handling. + +[Stack Overflow - Best Practices for Python Error Handling](https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python) - Insights into Python error handling best practices. diff --git a/content/syllabuses/data-eng-part-1.md b/content/syllabuses/data-eng-part-1.md index fef99aad8..aa7c76439 100644 --- a/content/syllabuses/data-eng-part-1.md +++ b/content/syllabuses/data-eng-part-1.md @@ -14,6 +14,8 @@ _db_id: 4 - {{< contentlink path="topics/code-reviews/part-2-author" >}} - {{< contentlink path="tech-big-picture/how-web-applications-work/part-5" >}} - {{< contentlink path="topics/high-performance-dev-teams" >}} +- {{< contentlink path="error-handling/python-error-handling" flavour="python" >}} +- {{< contentlink path="error-handling/error-handling-assignment" >}} - {{< contentlink path="specific-skill-success-criteria/introduction-to-assessments" >}} - {{< contentlink path="specific-skill-success-criteria/functions-and-return" flavour="python" >}} - {{< contentlink path="projects/katas/level-2" flavour="python" >}} @@ -35,4 +37,3 @@ _db_id: 4 - {{< contentlink path="projects/understanding-loops" flavour="python" >}} - {{< contentlink path="topics/solo-learn/python/python-intermediate/5-working-with-files" >}} - {{< contentlink path="topics/solo-learn/python/python-intermediate/5-working-with-files-project" >}} - diff --git a/content/syllabuses/data-sci-part-1.md b/content/syllabuses/data-sci-part-1.md index ed3f5964a..54d76218a 100644 --- a/content/syllabuses/data-sci-part-1.md +++ b/content/syllabuses/data-sci-part-1.md @@ -13,6 +13,8 @@ title: Data Science - part 1 - {{< contentlink path="topics/code-reviews/part-2-author" >}} - {{< contentlink path="topics/solo-learn/python/python-intermediate/1-collection-types" >}} - {{< contentlink path="topics/solo-learn/python/python-intermediate/1-collection-types-project" >}} +- {{< contentlink path="error-handling/python-error-handling" flavour="python" >}} +- {{< contentlink path="error-handling/error-handling-assignment" >}} - {{< contentlink path="projects/katas/level-2" flavour="python" >}} - {{< contentlink path="specific-skill-success-criteria/introduction-to-assessments" >}} - {{< contentlink path="specific-skill-success-criteria/functions-and-return" flavour="python" >}} diff --git a/content/syllabuses/web-dev-part-1.md b/content/syllabuses/web-dev-part-1.md index 0535232cc..b505e2a34 100644 --- a/content/syllabuses/web-dev-part-1.md +++ b/content/syllabuses/web-dev-part-1.md @@ -36,7 +36,8 @@ _db_id: 3 - {{< contentlink path="topics/free-code-camp/responsive-web-design/5-responsive-web-design-principles" >}} - {{< contentlink path="topics/free-code-camp/responsive-web-design/6-css-flexbox" optional="1" >}} - {{< contentlink path="topics/free-code-camp/javascript-data-structures-and-algorithms/3-regular-expressions" >}} -- {{< contentlink path="topics/javascript-error-handling" flavour="javascript" >}} +- {{< contentlink path="error-handling/javascript-error-handling" flavour="javascript" >}} +- {{< contentlink path="error-handling/error-handling-assignment" >}} - {{< contentlink path="projects/tdd/password-checker/part1" flavour="javascript" >}} - {{< contentlink path="topics/tech-terminology" >}} - {{< contentlink path="topics/free-code-camp/javascript-data-structures-and-algorithms/8-functional-programming" >}} From 396c66828ad82fbc9976b185f184e16be733ffdd Mon Sep 17 00:00:00 2001 From: TheMaja Date: Mon, 2 Oct 2023 16:58:26 +0200 Subject: [PATCH 2/4] added java content --- .../java-error-handling/_index.md | 105 ++++++++++++++++++ content/syllabuses/java-part-1.md | 2 + 2 files changed, 107 insertions(+) create mode 100644 content/error-handling/java-error-handling/_index.md diff --git a/content/error-handling/java-error-handling/_index.md b/content/error-handling/java-error-handling/_index.md new file mode 100644 index 000000000..b53893fe5 --- /dev/null +++ b/content/error-handling/java-error-handling/_index.md @@ -0,0 +1,105 @@ +--- +content_type: topic +flavours: + - java +ready: true +tags: + - java +title: Java Error Handling Best Practices +--- + +## Exceptions are Valuable + +In Java, exceptions are like warning signals that activate when something goes wrong in your code. They are not to be ignored or casually written to a log; instead, they require proper handling. + +When your program encounters an issue, it can abruptly halt its execution. Java offers a way to tackle such problems using `try` and `catch` blocks. These blocks assist you in managing errors gracefully, preventing your program from crashing and enabling you to gather information about the encountered issue. + +## Common Error Handling Pitfalls + +Let's examine some typical mistakes made when handling errors: + +- Simply printing errors + +```java +try { + // Code... +} catch (Exception error) { + System.out.println(error); +} +``` + +- Printing errors with added formatting + +```java +try { + // Code... +} catch (Exception error) { + System.out.println("Error: " + error); +} +``` + +- Returning errors + +```java +try { + // Code... +} catch (Exception error) { + return error; +} +``` + +- Catching specific exceptions just to re-throw them without additional context + +```java +try { + // Code... +} catch (IllegalArgumentException error) { + throw error; +} +``` + +- Catching and ignoring exceptions + +```java +try { + // Code... +} catch (Exception error) { + throw new Exception("A new error, possibly unrelated to the caught error"); +} +``` + +## Knowing When to Catch Errors + +Understanding when to catch errors is vital. You should only catch errors that you can effectively manage. If you cannot handle the error, it is better to let it propagate up to the caller. This becomes especially critical when developing libraries or modules used by other programmers. Catching an error you cannot handle hides the error from the caller, potentially causing unexpected behaviour and bugs. + +## Creating Custom Exceptions + +In Java, you can create custom exceptions when you precisely know what went wrong or when you want to provide more context for an existing exception. + +There are various ways to throw custom exceptions: + +Simply throwing a caught exception or any object as an exception + +```java +// AVOID - It does not clearly indicate where the exception occurred, and you can throw almost anything as an exception +throw error; + +throw "Some error"; + +throw new Object(); +``` + +Using the Exception class: + +```java +// PREFERRED - It specifies the exception's location and allows you to include a message for clarity +throw new Exception(error); + +throw new Exception("Error message"); +``` + +## Additional Resources + +- [Oracle's Java Documentation on Exceptions](https://docs.oracle.com/javase/tutorial/essential/exceptions/): Explore Oracle's official documentation to deepen your understanding of handling exceptions in Java. + +- [Java Exception Handling: A Comprehensive Guide](https://www.baeldung.com/java-exceptions): A comprehensive guide on Java exceptions and error handling, covering various aspects and scenarios. diff --git a/content/syllabuses/java-part-1.md b/content/syllabuses/java-part-1.md index ba17cdbd0..7b8e52ed0 100644 --- a/content/syllabuses/java-part-1.md +++ b/content/syllabuses/java-part-1.md @@ -13,6 +13,8 @@ _db_id: 7 - {{< contentlink path="specific-skill-success-criteria/for-loops" flavour="java" >}} - {{< contentlink path="topics/solo-learn/java/3-arrays" >}} - {{< contentlink path="topics/clean-code" >}} +- {{< contentlink path="error-handling/java-error-handling" flavour="java" >}} +- {{< contentlink path="error-handling/error-handling-assignment" >}} - {{< contentlink path="topics/how-to-ask-for-help-with-your-code" >}} - {{< contentlink path="file-and-directory-naming/java" >}} - {{< contentlink path="file-and-directory-naming/file-directory-naming-assignment" >}} From ea6748970bade97fd7c9f0245e99616805f3dc22 Mon Sep 17 00:00:00 2001 From: TheMaja Date: Thu, 5 Oct 2023 19:52:44 +0200 Subject: [PATCH 3/4] reformated the questions --- .../error-handling-assignment/_index.md | 96 +++++++++++++++++-- .../python-error-handling/_index.md | 15 +-- 2 files changed, 91 insertions(+), 20 deletions(-) diff --git a/content/error-handling/error-handling-assignment/_index.md b/content/error-handling/error-handling-assignment/_index.md index 63f752271..7f04a51e1 100644 --- a/content/error-handling/error-handling-assignment/_index.md +++ b/content/error-handling/error-handling-assignment/_index.md @@ -7,8 +7,7 @@ flavours: - python prerequisites: hard: - - error-handling/javascript-error-handling - - error-handling/python-error-handling + - error-handling protect_main_branch: false ready: true submission_type: repo @@ -19,10 +18,95 @@ Please answer the following questions in markdown files and submit following the ## Questions -1. In your programming language, what is the fundamental purpose of error handling, and why is it crucial in software development? +1. What is the fundamental purpose of error handling? -2. In your programming language, what are some poor practices in error handling that developers should avoid? Provide examples if possible. +2. What are some poor practices in error handling that developers should avoid? Provide 1 example. -3. In your programming language, when should developers catch and handle errors, and what factors should influence this decision? +3. When should developers catch and handle errors? -4. In your programming language, demonstrate how to raise a custom error with a specific error message. Write a code snippet that showcases this, and explain why it's essential to include clear error messages in custom exceptions. +4. Take a look at the following code snippets: + +```java +// java +try { + // Code... +} catch (Exception error) { + System.out.println(error); +} + +try { + // Code... +} catch (Exception error) { + System.out.println("Error: " + error); +} + +try { + // Code... +} catch (Exception error) { + return error; +} + +try { + // Code... +} catch (IllegalArgumentException error) { + throw error; +} +``` + +```python +# python +try: + # Code... +except Exception as error: + print(error) + +try: + # Code... +except Exception as error: + print(f"Error: {error}") + +try: + # Code... +except Exception as error: + return error + +try: + # Code... +except ValueError as error: + raise error +``` + +```javascript +// javascript +try { + // Code... +} catch (error) { + console.error(error); +} + +try { + // Code... +} catch (error) { + console.log(`Error: ${error}`); +} + +try { + // Code... +} catch (error) { + return error; +} + +try { + // Code... +} catch (error) { + throw error; +} +``` + +please select and write one snippet that is a good example of error handling. + +## How to submit your work + +Please follow the following instructions to submit your work: + +{{< contentlink path="project-submission-instructions/markdown-questions" >}} diff --git a/content/error-handling/python-error-handling/_index.md b/content/error-handling/python-error-handling/_index.md index 96813e45b..ff8b8636b 100644 --- a/content/error-handling/python-error-handling/_index.md +++ b/content/error-handling/python-error-handling/_index.md @@ -71,20 +71,7 @@ It's important to understand when to catch errors. You should only catch errors In Python, you can create custom exceptions when you precisely understand what went wrong or when you want to provide more context to an existing exception. -There are different ways to raise custom exceptions: - -Just raising a caught exception or any object as an exception - -```python -# AVOID - It's not clear where the exception occurred; you can raise almost anything as an exception -raise error - -raise "Some error" - -raise {} -``` - -Using the Exception class: +By using the Exception class: ```python # PREFERRED - It indicates the location of the exception, and the constructor accepts a message for clarity From cbca9d01823f3480cc8c27f06375fde87f297549 Mon Sep 17 00:00:00 2001 From: TheMaja Date: Thu, 5 Oct 2023 19:59:40 +0200 Subject: [PATCH 4/4] uodated Hard Prerequisites --- content/error-handling/error-handling-assignment/_index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/content/error-handling/error-handling-assignment/_index.md b/content/error-handling/error-handling-assignment/_index.md index 7f04a51e1..ce7142755 100644 --- a/content/error-handling/error-handling-assignment/_index.md +++ b/content/error-handling/error-handling-assignment/_index.md @@ -7,7 +7,9 @@ flavours: - python prerequisites: hard: - - error-handling + - error-handling/java-error-handling + - error-handling/python-error-handling + - error-handling/javascript-error-handling protect_main_branch: false ready: true submission_type: repo