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 (# 2 of 8) (#2691)
* Edited instrucitons.apped to have better error handling instructions.
* Edited Jinja2 template and exemplar to have proper error message.
* Regenerated test file.
* Added instructions append with error handling message.
* Altered JinJa2 template for error handling and adjusted example to have error classes.
* Regenerated test cases.
* Updated stub wit new error classes.
* Added error handling instructions append.
* Adjusted JinJa2 template to test for specific message.
* Regenerated test file.
* Added instructions apped for error handling.
* Added instructions append for error handling and adjusted example and test file.
* Corrected syntax error.
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" an error when the scoring or playing rules are not followed. 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:
10
+
11
+
```python
12
+
# example when a bonus is attempted with an open frame
13
+
raiseIndexError("cannot throw bonus with an open tenth frame")
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 change cannot be made with the coins given. 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:
10
+
11
+
```python
12
+
# example when change cannot be made with the coins passed in
13
+
raiseValueError("can't make target with given coins")
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 two _custom exceptions_. One exception to be [raised](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement)/"thrown" when your circular buffer is **full**, and one for when it is **empty**. The tests will only pass if you customize appropriate exceptions, `raise` those exceptions, and include appropriate error messages.
12
+
13
+
To customize a `built-in exception`, create a `class` that inherits from that exception. When raising the custom exception with a message, write the message as an argument to the `exception` type:
14
+
15
+
```python
16
+
# subclassing the built-in BufferError to create BufferFullException
17
+
classBufferFullException(BufferError):
18
+
"""Exception raised when CircularBuffer is full.
19
+
20
+
message: explanation of the error.
21
+
22
+
"""
23
+
def__init__(self, message):
24
+
self.message = message
25
+
26
+
27
+
# raising a BufferFullException
28
+
raise BufferFullException("Circular buffer is full")
The Collatz Conjecture is only concerned with strictly positive integers, so your solution should raise a `ValueError` with a meaningful message if given 0 or a negative integer.
3
+
## Exception messages
4
+
5
+
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
+
The Collatz Conjecture is only concerned with **strictly positive integers**, so this exercise expects you to use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) and "throw" a `ValueError` in your solution if the given value is zero or a negative integer. 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:
10
+
11
+
```python
12
+
# example when argument is zero or a negative integer
13
+
raiseValueError("Only positive numbers are allowed")
0 commit comments