Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

written assignment error handling #1140

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions content/error-handling/error-handling-assignment/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
content_type: project
flavours:
- java
- javascript
- markdown
- python
prerequisites:
hard:
- error-handling/java-error-handling
- error-handling/python-error-handling
- error-handling/javascript-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. What is the fundamental purpose of error handling?

2. What are some poor practices in error handling that developers should avoid? Provide 1 example.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"In your programming language" should be mentioned somewhere in this sentence since we require an example


3. When should developers catch and handle errors?

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • "In your programming language", should also be included in this sentence.
  • We could also ask them to explain why.
  • Which example would you say is good in the snippets? I think the "good" example could be improved. E.g.
try {
  // Code...
} catch (error) {
	throw new Error(`Unexpected error occured while doing ${action} with ${computerSpecs}. Error stack: ${JSON.stringify(error.stack)}`)
}

This is just an example of rethrowing with more context while not ignoring the caught error


## How to submit your work

Please follow the following instructions to submit your work:

{{< contentlink path="project-submission-instructions/markdown-questions" >}}
105 changes: 105 additions & 0 deletions content/error-handling/java-error-handling/_index.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [Stack Overflow - best practices for JavaScript error handling](https://stackoverflow.com/questions/6484528/what-are-the-best-practices-for-javascript-error-handling)
89 changes: 89 additions & 0 deletions content/error-handling/python-error-handling/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
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.

By 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.
3 changes: 2 additions & 1 deletion content/syllabuses/data-eng-part-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand All @@ -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" >}}

2 changes: 2 additions & 0 deletions content/syllabuses/data-sci-part-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
2 changes: 2 additions & 0 deletions content/syllabuses/java-part-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
3 changes: 2 additions & 1 deletion content/syllabuses/web-dev-part-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down