-
Notifications
You must be signed in to change notification settings - Fork 85
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
TheMaja
wants to merge
4
commits into
develop
Choose a base branch
from
written_assignment_error_handling
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
content/error-handling/error-handling-assignment/_index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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" >}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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