print "start"
try: #start a try block
print "Inside try block"
raise Exception(99) #raise an error
print "This will not execute"
except Exception,i: #catch an error
print "Caught an exception -- value is:",
print i
print "end"
import sys
def main():
print "start"
try: #start a try block
print "Inside try block"
raise Exception(99) #raise an error
print "This will not execute"
except Exception,i: #catch an error
if isinstance(i,float):
print "Caught an exception -- value is:",
print i
else:
print "Abnormal program termination"
return
print "end"
main()
def Xtest(test):
print "Inside Xtest, test is: ",test
if(test):
raise Exception(test)
print "start"
try: #start a try block
print "Inside try block"
Xtest(0)
Xtest(1)
Xtest(2)
except Exception,i: #catch an error
print "Caught an exception -- value is:",
print i
print "end"
def Xhandler(test):
try:
if(test):
raise Exception(test)
except Exception,i:
print "Caught One! Ex #:",i
print "start"
Xhandler(1)
Xhandler(2)
Xhandler(0)
Xhandler(3)
print "end"
class MyException:
def __init__(self,s):
self.str_what=s
#Variable declaration
a=None
b=None
try:
print "Enter numerator and denominator:"
#User-input
a=10
b=0
if not(b):
raise MyException("Cannot divide by zero!")
else:
print "Quotient is",a/b
except MyException as e: #catch an error
print e.str_what
class MyException:
def __init__(self,s):
self.x=s
def Xhandler(test):
try:
if(test):
raise MyException(test)
else:
raise MyException("Value is zero")
except MyException as i:
if isinstance(i.x,int):
print "Caught One! Ex #:",i.x
else:
print "Caught a string:",
print i.x
print "start"
Xhandler(1)
Xhandler(2)
Xhandler(0)
Xhandler(3)
print "end"
class B:
pass
class D(B):
pass
derived=D()
try:
raise B()
except B as b:
print "Caught a base class."
except D as d:
print "This wont execute."
def Xhandler(test):
try:
if test==0:
raise Exception(test) #throw int
if test==1:
raise Exception('a') #throw char
if test==2:
raise Exception(123.23) #throw double
except: #Catches all exceptions
print "Caught One!"
print "start"
Xhandler(0)
Xhandler(1)
Xhandler(2)
print "end"
class MyException:
def __init__(self,s):
self.x=s
def Xhandler(test):
try:
if test==0:
raise MyException(test) #throw int
if test==1:
raise MyException('a') #throw char
if test==2:
raise MyException(123.23) #throw double
except MyException as i:
if isinstance(i.x,int): #catch an int exception
print "Caught",i.x
else: #catch all other exceptions
print "Caught One!"
print "start"
Xhandler(0)
Xhandler(1)
Xhandler(2)
print "end"
class MyException:
def __init__(self,s):
self.x=s
#This function can only throw ints, chars and doubles
def Xhandler(test):
if test==0:
raise MyException(test) #throw int
if test==1:
raise MyException('a') #throw char
if test==2:
raise MyException(123.23) #throw double
print "start"
try:
Xhandler(0)
except MyException as i:
if isinstance(i.x,int): #catch an int exception
print "Caught int"
elif isinstance(i.x,str): #catch a char exception
print "Caught char"
elif isinstance(i.x,float): #catch a float exception
print "Caught double"
print "end"
def Xhandler():
try:
raise Exception("hello") #throw a char *
except Exception,c: #catch a char *
print "Caugh char * inside Xhandler"
raise #rethrow char * out of function
print "start"
try:
Xhandler()
except Exception,c:
print "Caught char * inside main"
print "end"
from ctypes import *
#Variable declaration
p=[]
try:
for i in range(32):
p.append(c_int())
except MemoryError,m:
print "Allocation failure."
for i in range(32):
p[i]=i
for i in range(32):
print p[i],
from ctypes import *
#Variable declaration
p=[]
for i in range(32):
p.append(c_int())
if not(p):
print "Allocation failure."
for i in range(32):
p[i]=i
for i in range(32):
print p[i],
class three_d:
def __init__(self,i=0,j=0,k=0): #3=D coordinates
if(i==0 and j==0 and k==0):
self.x=self.y=self.z=0
print "Constructing 0, 0, 0"
else:
self.x=i
self.y=j
self.z=k
print "Constructing",i,",",j,",",k
def __del__(self):
print "Destructing"
#new overloaded relative to three_d.
def __new__(typ, *args, **kwargs):
obj = object.__new__(typ, *args, **kwargs)
return obj
def show(self):
print self.x,",",
print self.y,",",
print self.z
p1=[]*3
p2=[]
try:
print "Allocating array of three_d objects."
for i in range(3): #allocate array
p1.append(three_d())
print "Allocating three_d object."
p2.append(three_d(5,6,7)) #allocate object
except MemoryError:
print "Allocation error"
p1[2].show()
p2[0].show()
for i in xrange(2,-1,-1):
del p1[i] #delete array
print "Deleting array of thee_d objects."
del p2[0] #delete object
print "Deleting three_d object."