Unit 5 - Principles of Programming Languages - Www.rgpvnotes.in
Unit 5 - Principles of Programming Languages - Www.rgpvnotes.in
Unit 5
Exception Handling
Exceptions - An exception (or exceptional event) is a problem that arises during the execution of a program.
When an exception occurs the normal flow of the program is disrupted and the program/application
terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.
Exception Propagation
An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the
previous method, If not caught there, the exception again drops down to the previous method, and so on until
they are caught or until they reach the very bottom of the call stack. This is called exception propagation.
Example
class ExceptionPropagation{
void m(){
int data = 10/0;
}
void n(){
m();
}
void p(){
try{
n();
}catch(Exception e){
System.out.println("exception handled");
}
}
public static void main(String args[]){
ExceptionPropagation obj = new ExceptionPropagation();
obj.p();
System.out.println("normal flow...");
}
}
Output:
exception handled
normal flow...
Syntax
try
{
statements;
... ... ...
throw exception;
}
Multiple catch exception statements are used when a user wants to handle different exceptions differently. For
this, a user must include catch statements with different declaration.
Syntax
try
{
body of try block
}
}
catch (int e)
{
cout << "Exception: Division by zero";
}
catch (char st)
{
cout << "Exception: Division is less than 1";
}
catch(...)
{
cout << "Exception: Unknown";
}
getch();
return 0;
}
This program demonstrates how exceptions are handled in C++. This program performs division operation. Two
numbers are entered by user for division operation. If the dividend is zero, then division by zero will cause
exception which is thrown into catch block. If the answer is less than 0, then exception "Division is less than 1"
is thrown. All other exceptions are handled by the last catch block throwing "Unknown" exception.
Output
Enter 2 numbers: 8 5
a/b = 1.6
Enter 2 numbers: 9 0
Exception: Division by zero
Enter 2 numbers: -1 10
Exception: Division is less than 1
Throws/Throw Keywords
If a method does not handle a checked exception, the method must declare it using the throws keyword. The
throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the
throw keyword.
throws is used to postpone the handling of a checked exception and throw is used to invoke an exception
explicitly.
The following method declares that it throws a RemoteException −
Example
import java.io.*;
public class className {
Finally Block
The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of
occurrence of an Exception.
Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what
happens in the protected code.
A finally block appears at the end of the catch blocks and has the following syntax −
Syntax
try {
// Protected code
}catch(ExceptionType1 e1) {
// Catch block
}catch(ExceptionType2 e2) {
// Catch block
}catch(ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}
Example
public class ExcepTest {
}
}
This will produce the following result −
Output
Exception thrown: java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
Logic Programming
Logic programming is a computer programming paradigm in which program statements expresses facts and
rules about problems within a system of formal logic. Rules are written as logical clauses with a head and a
body; for instance, "H is true if B1, B2, and B3 are true." Facts are expressed similar to rules, but without a
body; for instance, "H is true."
Some logic programming languages such as Data log and Answer Set Programming (ASP) are purely declarative
they allow for statements about what the program should accomplish, with no explicit step-by-step
instructions about how to do so. Others, such as Prolog, are a combination of declarative and imperative —
Like LISP, LP is about manipulation of symbols, and thus has potential in AI applications.
specification.
Prolog consists of a series of rules and facts. A program is run by presenting some query and seeing if this can
be proved against these known rules and facts.
Simple Facts
In Prolog we can make some statements by using facts. Facts either consist of a particular item or a relation
between items. For example we can represent the fact that it is sunny by writing the program:
sunny.
We can now ask a query of Prolog by asking
?- sunny.
?- is the Prolog prompt. To this query, Prolog will answer yes. sunny is true because (from above) Prolog
matches it in its database of facts.
Facts have some simple rules of syntax. Facts should always begin with a lowercase letter and end with a full
stop. The facts themselves can consist of any letter or number combination, as well as the underscore _
character. However, names containing the characters -,+,*,/, or other mathematical operators should be
avoided.
Examples of Simple Facts
Here are some simple facts about an imaginary world. /* and */ are comment delimiters
john_is_cold. /* john is cold */
raining. /* it is raining */
john_Forgot_His_Raincoat. /* john forgot his raincoat */
Rule Statements
• Used for the hypotheses
• Headed Horn clause
• Right side: antecedent (if part)
– May be single term or conjunction
• Left side: consequent (then part)
– Must be single term
• Conjunction: multiple terms separated by logical AND operations (implied)
Example Rules
Ancestor (mary,shelley):- mother(mary,shelley).
• Can use variables (universal objects) to generalize meaning:
parent(X,Y):- mother(X,Y).
parent(X,Y):- father(X,Y).
grandparent(X,Z):- parent(X,Y), parent(Y,Z).
sibling(X,Y):- mother(M,X), mother(M,Y),
father(F,X), father(F,Y).
Functional programming languages are specially designed to handle symbolic computation and list processing
applications. Functional programming is based on mathematical functions. Some of the popular functional
programming languages include: LISP, Python etc. Functional programming languages are categorized into two
groups, i.e. −
Pure Functional Languages − These t pes of fu tio al la guages support o l the fu tio al
paradig s. For e a ple − Haskell.
Impure Functional Languages − These t pes of fu tio al languages support the functional paradigms
a d i perati e st le progra i g. For e a ple − LISP.
• Referential Transparency - In an FPL, the evaluation of a function always produces the same result given
the same parameters.
Introduction to 4GL
A fourth-generation programming language (4GL) is any computer programming language that belongs to a
class of languages envisioned as advancement upon third-generation programming languages (3GL). Each of
the programming language generations aims to provide a higher level of abstraction of the internal computer
hardware details, making the language more programmer-friendly, powerful and versatile. While the definition
of 4GL has changed over time, it can be typified by operating more with large collections of information at
once rather than focusing on just bits and bytes. Languages claimed to be 4GL may include support for
database management, report generation, mathematical optimization, GUI development, or web
development. Some researchers state that 4GLs are a subset of domain-specific languages
Advantages of 4GL
1. Programming productivity is increased. One line of 4GL code is equivalent to several lines of 3GL code.
2. System development is faster.
3. Program maintenance is easier.
4. The finished system is more likely to be what the user envisaged, if a prototype is used and the user is
involved throughout the development.
5. End user can often develop their own applications.
6. Programs developed in 4GLs are more portable than those developed in other generation of languages.
7. Documentation is improved because many 4GLs are self documenting.
Disadvantages of 4GL
1. The programs developed in the 4GLs are executed at a slower speed by the CPU.
2. The programs developed in these programming languages need more space in the memory of the
computer system.