Chapter Twelve : Error handling

EXAMPLE 12.1 page no : 135

In [7]:
import socket
import sys
import os
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM,0)

if(sock < 0):
    pass

EXAMPLE 12.2 page no : 136

In [2]:
class EmcException:
    def operator(self,n):
        pass

class EmcSystemException( EmcException):
    def __init__(self,m):
        pass
    
    def emcSocket(self,family,t,protocol):
        # create socket
        socketfd = socket(family, t, protocol);
        if (socketfd < 0): # check status value
            raise EmcSystemException("emcSocket");
        return socketfd;

EXAMPLE 12.3 page no : 139

In [13]:
def __init__(self,cp):
    self.lengthM = len(cp)
    self.cpM = cp

EXAMPLE 12.5 page no : 141

In [4]:
class EmcLog:
    def __init__(self,filename):
        pass

    def message(self,EmcString):
        pass

    def __del__(self):
        if uncaught_exception():
            self.flush()
            
    def flush(self):
        if ( not uncaught_exception()):
            raise EmcSystemException("EmcLog::flush()")

EXAMPLE 12.6 page no : 142

In [5]:
class EmcException:
    def __init__(self,m):
        maxSizeM = 100
        actualLength = len(m);
        self.lengthM = min(maxSizeM,actualLength);
        self.messageM = message

EXAMPLE 12.7

In [6]:
def f():
    ip = 1
    g(ip)


def g(i):
    raise Exception(i)

EXAMPLE 12.8 page no : 146

In [7]:
def f():
    ip = 1
    g(ip)

def g(i):
    raise Exception(i)

EXAMPLE 12.9 page no :146

In [8]:
def f():
    ip = 1
    g(ip)

EXAMPLE 12.10 page no : 148

In [9]:
class EmcStack:
    defaultSizeM = 100
    def __init__(self,size = 100):
        pass

    def __del__(self):
        pass

    def operator(self,s):
        pass

    def empty(self):
        pass
        
    def top(self):
        pass

    def push(self,i):
        pass
    
    def pop(self):
        pass

EXAMPLE 12.12 page no : 151

In [10]:
class EmcException:
    def __init__(self,m):
        pass
      
    def operator(self,o,EmcException):
        pass

    def printOn(self,o):   
        print self.messageM;

EXAMPLE 12.13 page no : 153

In [11]:
try:
    socketfd = emcSocket(AF_UNIX, SOCK_STREAM, 0);
except Exception,e:
    print e
name 'emcSocket' is not defined

EXAMPLE 12.14 page no : 155

In [12]:
def at(s,pos):
    if (pos > len(s)):
        raise Exception;
In [ ]: