Skip to content

Commit

Permalink
new additions
Browse files Browse the repository at this point in the history
  • Loading branch information
mraza007 committed Jan 12, 2019
1 parent a799526 commit c59c6e3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This repo is focused towards people who are learning Python for the first time.
* [Tuples & Sets](markdown-version/tuples_and_sets.md)
* [Functions](markdown-version/functions.md)
* [Lambdas](markdown-version/lambdas.md)
* [Debugging](markdown-version/debugging.md)


Feel Free to fork this repo and contribute if you think I missed something.
Expand Down
54 changes: 54 additions & 0 deletions markdown-version/debugging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Debugging Python Code
- Try and Else Block
- Pdb
- set trace method

## Common Python Errors
- `SyntaxError`
- `NameError` This usually occurs when a variable is not defined or it hasn't been assigned yet.
- `TypeError` This usually occurs when an operation or function is applied to a wrong type.
- `IndexError` This occurs when we are trying to access an element in a list using invalid index.
- `ValueError` This occurs when a built in operation or function receives an arguement that has the right type but an inappropiate value
- `KeyError` This occurs when a dictionary doesn't have a specific key.
- `AttributeError` This occurs when a variable doesn't have an attribute.

## Raising Our own Exceptions
- We can raise our own error exceptions

```python
raise ValueError('invalid Error')
```

- Lets say we have a simple function and we want it to have our own exception.

```python
def color(text,color):
if type(text) is not str:
raise TypeError('Enter a valid String')
print(f'This {text} is using {color}.')
```

## Handling Errors
- In python we can handle errors using `try` and `except`

```python
try:
x
except ValueError:
print("there's a error")
```

`Else` and `Finally`.
`Finally` would run no matter what.

```python
# a small program
try:
x = int(input("Enter a number"))
except:
print("Enter a valid Number")
else:
print("Thanks for a valid number")
finally:
print("I will run no matter what hehehe")
```

0 comments on commit c59c6e3

Please sign in to comment.