Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
pcafrica committed Feb 17, 2025
1 parent ea405a0 commit 4de109e
Showing 1 changed file with 106 additions and 106 deletions.
212 changes: 106 additions & 106 deletions lectures/3/3.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ _class: titlepage

###### Pasquale Claudio Africa, Dario Coscia

###### 14 Feb 2025
###### 18 Feb 2025

---

# Outline

1. Scientific data types
2. The role of Python in modern scientific computing
3. Dependency management
4. Error handling
3. Error handling
4. Dependency management
5. Docker
6. Continuous Integration/Continuous Deployment

Expand Down Expand Up @@ -484,6 +484,107 @@ _class: titlepage
_class: titlepage
-->
# Error handling
---
# Exceptions
If something goes wrong, we don't want our code to crash - we want it to **fail gracefully**. In Python, this can be accomplished using `try`/`except`. Here is a basic example:
```python
this_variable_does_not_exist
print("Another line") # Code fails before getting to this line.
```
> NameError: name 'this_variable_does_not_exist' is not defined
---
# `try-except`
```python
try:
this_variable_does_not_exist
except:
pass # Do nothing.
print("You did something bad! But I won't raise an error.")
print("Another line")
```
You did something bad! But I won't raise an error.
Another line
Python tries to execute the code in the `try` block. If an error is encountered, we *catch* this in the `except` block (also called `try`/`catch` in other languages). There are many different error types, or **exceptions** - we saw `NameError` above.
```python
5 / 0
```
> ZeroDivisionError: division by zero
---
# A complete example
```python
x = 2
y = 0
try:
result = x / y
except ZeroDivisionError:
print("Sorry! You are dividing by zero.")
else:
print("Yeah! Your answer is: ", result)
finally:
# This block is always executed
# regardless of exception generation.
print('This is always executed')
```
---
# More exception types
```python
my_list = [1, 2, 3]
my_list[5]
```
> IndexError: list index out of range
```python
my_tuple = (1,2,3)
my_tuple[0] = 0
```
> TypeError: 'tuple' object does not support item assignment
---
# Raise exceptions
We can also write code that raises an exception on purpose, using `raise`:
```python
def add_one(x):
if not isinstance(x, float) and not isinstance(x, int):
raise TypeError(f"Sorry, x must be numeric, you entered a {type(x)}.")
return x + 1
add_one("blah")
```
> TypeError: Sorry, x must be numeric, you entered a <class 'str'>.
This is useful when your function is complicated and would fail in a complicated way, with a weird error message. You can make the cause of the error much clearer to the *user* of the function.
Finally, we can even define our own exception types by inheriting from the `Exception` class!
---
<!--
_class: titlepage
-->
# Dependency management
---
Expand Down Expand Up @@ -648,107 +749,6 @@ dependencies:
_class: titlepage
-->
# Error handling
---
# Exceptions
If something goes wrong, we don't want our code to crash - we want it to **fail gracefully**. In Python, this can be accomplished using `try`/`except`. Here is a basic example:
```python
this_variable_does_not_exist
print("Another line") # Code fails before getting to this line.
```
> NameError: name 'this_variable_does_not_exist' is not defined
---
# `try-except`
```python
try:
this_variable_does_not_exist
except:
pass # Do nothing.
print("You did something bad! But I won't raise an error.")
print("Another line")
```
You did something bad! But I won't raise an error.
Another line
Python tries to execute the code in the `try` block. If an error is encountered, we *catch* this in the `except` block (also called `try`/`catch` in other languages). There are many different error types, or **exceptions** - we saw `NameError` above.
```python
5 / 0
```
> ZeroDivisionError: division by zero
---
# A complete example
```python
x = 2
y = 0
try:
result = x / y
except ZeroDivisionError:
print("Sorry! You are dividing by zero.")
else:
print("Yeah! Your answer is: ", result)
finally:
# This block is always executed
# regardless of exception generation.
print('This is always executed')
```
---
# More exception types
```python
my_list = [1, 2, 3]
my_list[5]
```
> IndexError: list index out of range
```python
my_tuple = (1,2,3)
my_tuple[0] = 0
```
> TypeError: 'tuple' object does not support item assignment
---
# Raise exceptions
We can also write code that raises an exception on purpose, using `raise`:
```python
def add_one(x):
if not isinstance(x, float) and not isinstance(x, int):
raise TypeError(f"Sorry, x must be numeric, you entered a {type(x)}.")
return x + 1
add_one("blah")
```
> TypeError: Sorry, x must be numeric, you entered a <class 'str'>.
This is useful when your function is complicated and would fail in a complicated way, with a weird error message. You can make the cause of the error much clearer to the *user* of the function.
Finally, we can even define our own exception types by inheriting from the `Exception` class!
---
<!--
_class: titlepage
-->
# Introduction to Docker
---
Expand Down Expand Up @@ -982,7 +982,7 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Set up Python environment
run: |
Expand Down Expand Up @@ -1013,7 +1013,7 @@ jobs:
sphinx-build -b html . _build/html
- name: Upload documentation
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: documentation
path: docs/_build/html/
Expand Down

0 comments on commit 4de109e

Please sign in to comment.