Skip to content

Commit c81aad9

Browse files
committed
add Python 'Error' question + answer + Exception ansers
1 parent deaf373 commit c81aad9

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

README.md

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,7 @@ The immutable data types are:
16721672
String
16731673
Bool
16741674
Tuple
1675+
Frozenset
16751676

16761677
You can usually use the function hash() to check an object mutability, if it is hashable it is immutable, although this does not always work as intended as user defined objects might be mutable and hashable
16771678
</b></details>
@@ -1760,7 +1761,70 @@ There is a more advanced python feature called MetaClasses that aid the programm
17601761
</b></details>
17611762

17621763
<details>
1763-
<summary>What is an exception? What types of exceptions are you familiar with?</summary><br><b>
1764+
<summary> What is an error? What is an exception? What types of exceptions are you familiar with?</summary><br><b>
1765+
1766+
```
1767+
1768+
# Note that you generally don't need to know the compiling process but knowing where everything comes from
1769+
# and giving complete answers shows that you truly know what you are talking about.
1770+
1771+
Generally, every compiling process have a two stops.
1772+
- Analysis
1773+
- Code Generation.
1774+
1775+
Analysis can be broken into:
1776+
1. Lexycal analysis (Tokenizes source code)
1777+
2. Syntactic analysis (Check whether the tokens are legal or not, tldr, if syntax is correct)
1778+
1779+
for i in 'foo'
1780+
^
1781+
SyntaxError: invalid syntax
1782+
1783+
We missed ':'
1784+
1785+
1786+
3. Semantic analysis (Contextual analysis, legal syntax can still trigger errors, did you try to divide by 0,
1787+
hash a mutable object or use an undeclared function?)
1788+
1789+
1/0
1790+
ZeroDivisionError: division by zero
1791+
1792+
These three analysis steps are the responsible for error handlings.
1793+
1794+
The second step would be responsible for errors, mostly syntax errors, the most common error.
1795+
The third step would be responsible for Exceptions.
1796+
1797+
As we have seen, Exceptions are semantic errors, there are many builtin Exceptions:
1798+
1799+
ImportError
1800+
ValueError
1801+
KeyError
1802+
FileNotFoundError
1803+
IndentationError
1804+
IndexError
1805+
...
1806+
1807+
You can also have user defined Exceptions that have to inherit from the `Exception` class, directly or indirectly.
1808+
1809+
Basic example:
1810+
1811+
class DividedBy2Error(Exception):
1812+
def __init__(self, message):
1813+
self.message = message
1814+
1815+
1816+
def division(dividend,divisor):
1817+
if divisor == 2:
1818+
raise DividedBy2Error('I dont want you to divide by 2!')
1819+
return dividend / divisor
1820+
1821+
division(100, 2)
1822+
1823+
>>> __main__.DividedBy2Error: I dont want you to divide by 2!
1824+
1825+
```
1826+
1827+
17641828
</b></details>
17651829

17661830
<details>
@@ -2151,7 +2215,7 @@ func main() {
21512215
</summary><br><b>
21522216

21532217
It looks what unicode value is set at 101 and uses it for converting the integer to a string.
2154-
If you want to get "101" you should use the package "strconv" and repalce <code>y = string(x)</code> with <code>y = strconv.Itoa(x)</code>
2218+
If you want to get "101" you should use the package "strconv" and replace <code>y = string(x)</code> with <code>y = strconv.Itoa(x)</code>
21552219
</b></details>
21562220

21572221
## Mongo

0 commit comments

Comments
 (0)