You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Practice Exercises]: Add Better Error Handling Instructions & Tests for Error Raising Messages (# 3 of 8) (#2692)
* Added instruciton append for error handling, updated JinJa2 template and example and regenerated test file.
* Added instruction append on error handling and edited JinJa2 template and example.py. Regenerated test file.
* Added instructions append for error handling, updated JinJa2 template and example, and regenerated test files.
* Added instructions append for error handling, updated JinJa2 template and example, and regenerated test files.
* Regenerated go-counting test file. Again.
* EOL
Co-authored-by: Victor Goff <keeperotphones@gmail.com>
Sometimes it is necessary to both [customize](https://docs.python.org/3/tutorial/errors.html#user-defined-exceptions) and [`raise`](https://docs.python.org/3/tutorial/errors.html#raising-exceptions) exceptions in your code. When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging.
6
+
7
+
Custom exceptions can be created through new exception classes (see [`classes`](https://docs.python.org/3/tutorial/classes.html#tut-classes) for more detail.) that are typically subclasses of [`Exception`](https://docs.python.org/3/library/exceptions.html#Exception).
8
+
9
+
For situations where you know the error source will be a derivative of a certain exception type, you can choose to inherit from one of the [`built in error types`](https://docs.python.org/3/library/exceptions.html#base-classes) under the _Exception_ class. When raising the error, you should still include a meaningful message.
10
+
11
+
This particular exercise requires that you create a _custom exception_ to be [raised](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement)/"thrown" when the stack is not sufficiently filled. The tests will only pass if you customize an appropriate exception, `raise` that exception, and include appropriate error messages.
12
+
13
+
14
+
```python
15
+
# subclassing the Exception to create a StackUnderflowError
16
+
classStackUnderflowError(Exception):
17
+
"""Exception raised when Stack is not full.
18
+
message: explanation of the error.
19
+
"""
20
+
def__init__(self, message):
21
+
self.message = message
22
+
23
+
24
+
# raising a StackUnderflowError
25
+
raise StackUnderflowError("Insufficient number of items in stack")
26
+
```
27
+
28
+
Additionally, this exercise requires that you raise several `built-in exceptions` with error messages.
29
+
To raise a `built-in exception` with a message, write the message as an argument to the `exception` type:
Sometimes it is necessary to [raise an exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions). When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types](https://docs.python.org/3/library/exceptions.html#base-classes), but should still include a meaningful message.
6
+
7
+
This particular exercise requires that you use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) to "throw" a `ValueError` when given invalid coordinates. The tests will only pass if you both `raise` the `exception` and include a message with it.
8
+
9
+
To raise a `ValueError` with a message, write the message as an argument to the `exception` type:
0 commit comments