-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d9cb387
Showing
7 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Python 3 | ||
Test exceptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class MiError(Exception): | ||
def __init__(self, valor): | ||
self.valor = valor | ||
def __str__(self): | ||
return repr(self.valor) | ||
|
||
try: | ||
raise MiError(2*2) | ||
except MyError as e: | ||
print('Ocurrió mi excepción, valor:', e.encode('utf-8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# define Python user-defined exceptions | ||
class Error(Exception): | ||
"""Base class for other exceptions""" | ||
pass | ||
|
||
class ValueTooSmallError(Error): | ||
"""Raised when the input value is too small""" | ||
pass | ||
|
||
class ValueTooLargeError(Error): | ||
"""Raised when the input value is too large""" | ||
pass | ||
|
||
# our main program | ||
# user guesses a number until he/she gets it right | ||
|
||
# you need to guess this number | ||
number = 10 | ||
|
||
while True: | ||
try: | ||
i_num = int(input("Enter a number: ")) | ||
if i_num < number: | ||
raise ValueTooSmallError | ||
elif i_num > number: | ||
raise ValueTooLargeError | ||
break | ||
except ValueTooSmallError: | ||
print("This value is too small, try again!") | ||
print(" ") | ||
except ValueTooLargeError: | ||
print("This value is too large, try again!") | ||
print(" ") | ||
|
||
print("Congratulations! You guessed it correctly.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
while True: | ||
# Read int from console. | ||
denominator = int(input()) | ||
|
||
# Use int as denominator. | ||
try: | ||
i = 1 / denominator | ||
except: | ||
print("Error") | ||
else: | ||
print("OK") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def mistake(name): | ||
raise Exception(name+" caused exception") | ||
|
||
|
||
try: | ||
x=1/0 | ||
except ZeroDivisionError: | ||
print ("Tryed div by zero") | ||
print ("salir") | ||
mistake("error de conexion") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
try: | ||
f=open("abc") | ||
except: | ||
print("except hit") | ||
raise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
raise ZeroDivisionError |