0% found this document useful (0 votes)
24 views4 pages

Built in Exceptions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Built in Exceptions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

try:

a = 10/0
print (a)
except ArithmeticError:
print ("This statement is raising an arithmetic exception.")
else:
print ("Success.")

================

try:
a = [1, 2, 3]
print (a[3])
except LookupError:
print ("Index out of bound error.")
else:
print ("Success")

=============

class Attributes(object):
pass

object = Attributes()
print (object.attribute)

=============

while True:
data = input('Enter name : ')
print ('Hello ', data)

===========

import math

print (math.exp(1000))

=======

def my_generator():
try:
for i in range(5):
print ('Yielding', i)
yield i
except GeneratorExit:
print ('Exiting early')

g = my_generator()
print (g.next())
g.close()

==============
import module_does_not_exist

==========

from exceptions import Userexception

=========

array = [ 0, 1, 2 ]
print (array[3])

===========

array = { 'a':1, 'b':2 }


print (array['c'])

============

try:
print ('Press Return or Ctrl-C:',)
ignored = input()
except Exception, err:
print ('Caught exception:', err)
except KeyboardInterrupt, err:
print ('Caught KeyboardInterrupt')
else:
print ('No exception')

============

def fact(a):
factors = []
for i in range(1, a+1):
if a%i == 0:
factors.append(i)
return factors

num = 600851475143
print (fact(num))

==============

def func():
print ans

func()

==========

class BaseClass(object):
"""Defines the interface"""
def __init__(self):
super(BaseClass, self).__init__()
def do_something(self):
"""The interface, not implemented"""
raise NotImplementedError(self.__class__.__name__ + '.do_something')

class SubClass(BaseClass):
"""Implements the interface"""
def do_something(self):
"""really does something"""
print (self.__class__.__name__ + ' doing something!')

SubClass().do_something()
BaseClass().do_something()

===========

def func():
print (ans)

func()

==========

import sys

print ('Regular integer: (maxint=%s)' % sys.maxint)


try:
i = sys.maxint * 3
print ('No overflow for ', type(i), 'i =', i)
except OverflowError, err:
print ('Overflowed at ', i, err)

print()
print ('Long integer:')
for i in range(0, 100, 10):
print ('%2d' % i, 2L ** i)

print()
print ('Floating point values:')
try:
f = 2.0**i
for i in range(100):
print (i, f)
f = f ** 2
except OverflowError, err:
print ('Overflowed after ', f, err)

=================

try:
print (eval('python for everyone'))
except SyntaxError, err:
print ('Syntax error %s (%s-%s): %s' % \
(err.filename, err.lineno, err.offset, err.text))
print (err)

==============

arr = ('tuple', ) + 'string'


print (arr)

============

syntax to define custom exceptions,

class CustomError(Exception):
...
pass

try:
...

except CustomError:
...

ex:

# define Python user-defined exceptions


class InvalidAgeException(Exception):
"Raised when the input value is less than 18"
pass

# you need to guess this number


number = 18

try:
input_num = int(input("Enter a number: "))
if input_num < number:
raise InvalidAgeException
else:
print("Eligible to Vote")

except InvalidAgeException:
print("Exception occurred: Invalid Age")

You might also like