summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter24.ipynb
blob: 05dd848847bcc644a887fdd4ade2109c470300d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{
 "metadata": {
  "name": "",
  "signature": "sha256:70182e1ed50aea78649f1bef2946ce2b6e335126b84963ba06fce0660ac72886"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 24: Error Handling"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example tsignal.c, Page 634"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import signal\n",
      "import sys\n",
      "def handler(sig,frame):\n",
      "    print \"Handler called for signal\",sig\n",
      "print \"Installing handler for signal\", signal.SIGINT\n",
      "orig_handler=signal.signal(signal.SIGINT,handler)\n",
      "print \"Changing handler to SIG_IGN\"\n",
      "signal.signal(signal.SIGINT,signal.SIG_IGN)\n",
      "print \"Restoring original handler\"\n",
      "signal.signal(signal.SIGINT,orig_handler)\n",
      "print \"Program terminates normally\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Installing handler for signal 2\n",
        "Changing handler to SIG_IGN\n",
        "Restoring original handler\n",
        "Program terminates normally\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example tsetjmp.c, Page 636"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def main():\n",
      "    def f1():\n",
      "        print \"f1 begins\"\n",
      "        f2()\n",
      "        return\n",
      "        print \"f1 returns\"\n",
      "\n",
      "    def f2():\n",
      "        print \"f2 begins\"\n",
      "        #longjmp(env, 1)\n",
      "        setjmp=1\n",
      "        print \"Program terminates: longjmp called\"\n",
      "        return\n",
      "        print \"f2 returns\"\n",
      "\n",
      "    setjmp=0\n",
      "    if (setjmp == 0):\n",
      "        print \"setjmp returned 0\"\n",
      "    else:\n",
      "        print \"Program terminates: longjmp called\"\n",
      "    f1()\n",
      "    return\n",
      "    print \"Program terminates normally\"\n",
      "    \n",
      "if  __name__=='__main__':\n",
      "    main()\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "setjmp returned 0\n",
        "f1 begins\n",
        "f2 begins\n",
        "Program terminates: longjmp called\n"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}