summaryrefslogtreecommitdiff
path: root/mit/exceptions.py
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /mit/exceptions.py
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'mit/exceptions.py')
-rw-r--r--mit/exceptions.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/mit/exceptions.py b/mit/exceptions.py
new file mode 100644
index 0000000..2208484
--- /dev/null
+++ b/mit/exceptions.py
@@ -0,0 +1,68 @@
+#!/usr/bin/python3
+
+def my_file():
+ fname = input("File: ")
+
+ try:
+ fh = open(fname, 'r')
+ except IOError:
+ print("Could not open file " + fname)
+ else:
+ print("Open: Success! File: " + fname)
+
+ return fh
+
+def convert():
+ var = input("Type in: ")
+
+ try:
+ var = int(var)
+ except ValueError: # We can use exceptions to handle special cases
+ print("Converting to string")
+ var = str(var)
+
+ return var
+
+def convert_file():
+ fhandle = my_file()
+
+ my_list = []
+
+ for line in fhandle:
+ e = line.split(',')
+
+ for i in e:
+ try:
+ print("Trying as int")
+ my_list.append(int(i))
+ except ValueError:
+ print("Opsi, element is not a number, converting to string")
+ my_list.append(str(i))
+
+ print(my_list)
+ return my_list
+
+def exe():
+ try:
+ try:
+ raise Exception("0")
+ finally:
+ print("Foo")
+ except Exception as ex:
+ print(ex)
+
+def weird():
+ try:
+ raise TypeError("Deu merda")
+ except TypeError as foo:
+ print(foo)
+ except:
+ print("Exception")
+
+def simple():
+ try:
+ 'a' / 3
+ except TypeError as err:
+ print("Opsy, the error was: " + str(err))
+
+ print(type(err))