CS-602 PPL Unit-5
CS-602 PPL Unit-5
Unit-05/Lecture-01
Exception Handling
Exceptions are runtime unusual conditions that a program may encounter while executing.
Types of Exceptions
i. Synchronous Exception- The errors that are caused by the events in the control of
program are called as synchronous exceptions. Examples are: out of index, overflow.
ii. Asynchronous exception- The errors that are caused by the events beyond the
control of program are called as asynchronous exception. Examples are: keyboard
interrupts, out of memory.
Exception Propagation:-
If an exception is not handled in the subprogram in which it was raised, this exception is
propagated to the subprogram that called it. A handler for the exception is searched for in
the calling subprogram. If no handler is found there then the exception is propagated again.
This continues until either an exception handler is found, or the highest level of the current
task is reached, in which case the task is aborted.
An exception is a problem that arises during the execution of a program. A C++ exception is
a response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.
throw: A program throws an exception when a problem shows up. This is done using
a throw keyword.
catch: A program catches an exception with an exception handler at the place in a
program where you want to handle the problem. The catch keyword indicates the
catching of an exception.
try: A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.
2
Throwing Exceptions:
Exceptions can be thrown anywhere within a code block using throw statements. The
operand of the throw statements determines a type for the exception and can be any
expression and the type of the result of the expression determines the type of exception
thrown.
if( b == 0 )
return (a/b);
Catching Exceptions:
The catch block following the try block catches any exception. You can specify what type of
exception you want to catch and this is determined by the exception declaration that
appears in parentheses following the keyword catch.
try
// protected code
}catch( ExceptionName e )
}
3
#include <iostream>
if( b == 0 )
return (a/b);
int main ()
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
return 0;
}
4
Unit-05/Lecture-02
When an exception is thrown, the exception handlers are searched in order for an
appropriate match. The first handler that yields a match is excuted. After executing the
handler; the control goes to the first statement after the last catch block for that try. When
no match is found, the program is terminated.
#include<iostream.h>
#include<conio.h>
void test(int x)
try
if(x>0)
throw x;
else
throw 'x';
catch(int x)
catch(char x)
{
5
void main()
clrscr();
test(10);
test(0);
getch();
Output:
Unit-05/Lecture-03
Exceptions are errors which occur when the program is executing. Consider the Java
program below which divides two integers.
import java.util.Scanner;
class Division {
int a, b, result;
a = input.nextInt();
b = input.nextInt();
result = a / b;
Java provides a powerful way to handle such exceptions which is known as exception
handling. In it we write vulnerable code i.e. code which can throw exception in a separate
block called as try block and exception handling code in another block called catch block.
Following modified code handles the exception.
class Division {
int a, b, result;
7
a = input.nextInt();
b = input.nextInt();
// try block
try {
result = a / b;
// catch block
catch (ArithmeticException e) {
Whenever an exception is caught corresponding catch block is executed, For example above
code catches Arithmetic Exception only. If some other kind of exception is thrown it will not
be caught so it's the programmer work to take care of all exceptions as in our try block we
are performing arithmetic so we are capturing only arithmetic exceptions. A simple way to
capture any exception is to use an object of Exception class as other classes inherit
Exception class, see another example below:
class Exceptions {
try {
8
System.out.println(languages[c]);
catch (Exception e) {
System.out.println(e);
Output of program:
C++
Java
Perl
Python
java.lang.ArrayIndexOutOfBoundsException: 5
9
Unit-05/Lecture-04
LOGIC PROGRAMMING
A logic program consists of a set of axioms and a goal statement. The rules of inference are
applied to determine whether the axioms are sufficient to ensure the truth of the goal
statement. The execution of a logic program corresponds to the construction of a proof of
the goal statement from the axioms.
In the logic programming model the programmer is responsible for specifying the basic
logical relationships and does not specify the manner in which the inference rules are
applied. Thus
Logic programming is based on tuples. Predicates are abstractions and generalization of the
data type of tuples.
Languages used for logic programming are called Declarative languages becaue programs
written using them consists of declarations rather than assignments and control flow
statements. These declarations are actually statements or propositions in symbolic logic.
Declarative semantics is simpler than the semantics of imperative languages. For example,
the meaning of a given proposition in a logic programming language can be determined
from the statement itself. In an imperative language, the semantics of a simple assignment
statement requires examination of local declarations, knowledge of scoping rule of the
language, data type of variable etc.
father(bill,mary).
plus(2,3,5).
...
This fact states that the relation father holds between bill and mary. Another name for a
relationship is predicate.
10
Queries
?- father (bill,mary).
Introduction to Prolog
Ex. parent(X, Y): - mother(X, Y). This means that if x is a mother of y then x is the parent of y.
A goal statement is one that which requests an answer; the syntactic form of fact
statements and goal statements are identical
Ex. father(X, mike). This asks the question “who is the father of mike?”
parent(sue, bill).
parent(sue, james).
parent(sue, edith).
parent(fred, bill).
parent(fred, james).
parent(arthur, edith).
parent(mary, kylie).
parent(mary, jason).
parent(mary, matilda).
parent(james, kylie).
parent(james, jason).
11
parent(james, matilda).
parent(edith, david).
parent(william, david).
An example query would be? - ancestor (fred, david), "Is fred the ancestor of david" which
would return a no.
12
Unit-05/Lecture-05
Mathematical Functions
A mathematical function is a mapping of members of one set called the domain set to
another set called the range set.
A function definition specifies the domain and range sets along with the mapping. The
mapping is described by an expression; functions are always applied to a particular element
of the domain set and a function return an element of the range set.
In this definition the domain and range sets are real numbers. The parameter ‘x’
can represent any member of the domain set. The range element is obtained by evaluating
the function mapping expression with the domain element substituted for the occurrences
of the parameter.
function whose value is the first actual parameter function applied to the
result of the second. Functional composition is written as an expression1
using o as an operator.
H= f o g
g(x)= 3*x
h(x)= (3 * x) + 2
h(x) = 2 * x
f(x) = x / 2
3. Apply to all- Apply to all is a functional form that takes a single function as a
parameter. If applied to a list of arguments apply to all applies to its
functional parameter to each of the values in the list argument and collects
the result in a list or sequence. Apply to all is denoted by α.
Ex- h(x) = x * x
Unit-05/Lecture-06
“A type consists of set of elements called values together with a set of functions called
operations”.
Basic Types- A type is basic if its values are atomic- this is, if the values are treated as whole
elements with no internal structure. For Ex- the Boolean values in the set {true, false} are
basic values.
Operation on basic values- Basic values have no internal structure so the only operation
defined for all basic types is a comparison for equality; for example the equality 2 = 2 is true
and the equality 2 != 2 is false.
Product of types- The product A * B of two types A and B consists of ordered pairs written
as (a, b) where a is value of type A and b is value of type B. Thus
(1, “one”) is a pair consisting of the integer 1 and the string “one”
A product of n types A1 * A2.... * An consists of tuples written as (a1,
a2... an)
Operations on Pairs- Associated with pairs are operations called projection functions to
extract the first and second elements from a pair.
fun second( x, y) = y;
List of Elements- A list is a finite length sequence of elements. The type A list consists of all
list of elements, where each element belongs to type A.
2. FUNCTION DECLARATION
The keyword fun marks the beginning of function declaration, <name> is the function name,
<formal parameter> is a parameter name and <body> is an expression to be evaluated.
<name><actual parameter>
Thus, successor (2 + 3)
Recursive Functions
Ex- The following function len counts the number of elements in a list.
The function is recursive because the body contains an application of len; it is len (tl(x))
16
Unit-04/Lecture-07
1 GL was machine language or the level of instructions and data that the processor is
actually given to work on (string of 0s and 1s)
3 GL is a high level language such as C, C++, and Java. A compiler converts the statements of
a specific high level programming language into machine language. A 3GL requires a
considerable amount of programming knowledge.
For Ex- If we want to access records of employee having name ‘Smith ‘from emp table then
we have to write just the query
The languages like Oracle, VB++, VC++, and SQL etc are called as 4 GL languages. Most 4G
languages are used to access databases. They allow the programmer to define ‘what’ is
required without telling the computer ‘how’ to implement it.
Features of 4 GL
= 101 – 10
=91
Outermost Evaluation-
f (100) = if 100 > 100 then 100 -10 else f ( f (100 + 11))
else f(f(100+11)+11)
= 100 + 11 – 10
= 101
= 101 – 10
18
=91
Ex- E andalso F
Is false if E is false, it is true if both E and F are true. The evaluation of E andalso F proceeds
from left to right with F being evaluated only if E is true. Similarly the value of expression E
orelse F is true if E evaluates to true. If E is false then E is evaluated. In C operators & and |
performs short circuit evaluation of Boolean expression.
19
Q.7 What are Exceptions? How are they JUNE 2013 10 marks
handled in Java?
Q.8 Explain the rules for expression JUNE 2013 10 marks
evaluation in functional
programming?
Q.9 What is the purpose of instantiation JUNE 2013 10 marks
matching and unification with respect
to logic programming PROLOG?
20