You are on page 1of 23

Sequencer

CNG 242 Programming Language Concepts


Sequencer
• Usual control flow:
– a command followed by the other.
– Executed in sequence.
– single entrance - single exit
• Commands to change control flow and transfer
execution to another point: sequencers
– C1;C2 says first execute C1 then C2. But only if C1
terminates normally.
– If C1 executes a sequencer, C1 terminates abruptly
– C2 gets skipped!
• (if it is not the destination of the sequencer)
sequencer
• Sequencers makes multiple entry and multiple
exits possible
• Lowest level: Jumps
• Escapes
– Break,return, halt
• Exceptions
Example: Jumps
if (x>0) • Show the control flow
y=x++; chart.
else {
y=(-1) *x++; • If block has 2 exits now.
goto X; • For loop has 2 entries
} now.
for(i=0;i<y;i++)
{
z+=i;
X: z=y*z;
}
jumps
• goto L; L is a label
• Jumps are not self explanatory
– It may be forward jump or backward
– The destination may be pages away
• Causes spaghetti code since its flowchart is
tangled
– Some languages restrict jumps
– C: Labels are local to enclosing block. No jumps
allowed into the block.
– Newer languages avoid jumps
When arbitrary jumps are possible
• Jump out of a procedure body?
– Local variables?
– Activation records?
• Jump into a procedure body?
– What about recursion?

• Avoid JUMPs
• Still need single enrty multiple exits
Escapes
• Restricted jumps to out of textually enclosing
block(s)
– Destination of an escape is always the exit of the
enclosing block
– Single entry- multiple exits
• Depending on which enclosing block to jump out
of:
– Loop (or switch): break sequencer.
– Loops: exit sequencer.
– function: return sequencer.
– program: halt sequencer.
Escape sequencers
• return sequencer exist in most languages for
terminating the innermost function block.
– Destination: end of procedure/function body.
• halt sequencer either provided by operating
system or PL terminates the program.
– STOP in Fortran, exit() in C, System.exit() in Java
Need more…
• Escapes are useful. Implement single entry
multiple exits
• What if we need to jump out of a function block,
jump inside of a function block?
– We have seen GOTO is not ok
– Activation record, run-time stack? Possible only for
one direction if stack position can be recovered.
• Sometimes we need nonlocal jumps
– E.g. unexpected error occurs inside of many levels of
recursion. Need to jump to the outer-most related
caller function.
Recover from Abnormal situations
• Abnormal situation: program cannot continue normally
– Due to arithmetic overflow
– An I/O operation cannot be completed
– Malformed input
– Connection failed
• Need to transfer control to a handler that knows how to
recover
– A Robust program it the one that recovers from such
situations
– Need to jump out of the error occurring function
– Need a Controlled jumps out of multiple levels of function
calls to an outer control point
Recovery 1: return status
• Propagate errors with return values
• On return from a function, check the status flag –
it may indicate an abnormal situation
– Checking Status flags are usually ignored!
Recovery2: EXCEPTIONs
• Exception is an entity that represents an abnormal situation
• Any code that detects an abnormal situation occurred,
throws/raises the corresponding exception
– E.g. …. if (error_occured_in_fileIO) throw Exception
• Exception will be caught in another part of the program :
handler
– Handler recovers from abnormal situation
– After recovery, continue the next statement after the handler.
Command that threw the exception is never resumed.
• Exceptions cannot be ignored: if no handler catches them ,
program will halt.
General structure
try {
// protected code
// here an exception can be thrown
// or a function that may throw an exception can be
called
}catch( ExceptionName e1 ) {
// catch block
}catch( ExceptionName e2 ) {
// catch block
}catch( ExceptionName eN ) {
// catch block }
C++ simple example
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
// some other code will handle the exception
C++ simple example
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
//this throw terminates the function division
// this is the 2nd exit of the function
//after the recovery by a handler,
//this command will not be resumed.
}
return (a/b);
}
C++ catching an exception
int main () {
int x = 50; int y = 0;
double z = 0;
try {
z = division(x, y); //may throw an exception
cout << z << endl;
}catch (const char* msg) { //matches the string
cerr << msg << endl; //handling the exception
}
//program is in a normal situation
// either no error occurred or the error is recovered
return 0;
}
• The catch format is similar to a regular
function that always has at least one
parameter
• The type of this parameter is very important,
since the type of the argument passed by the
throw expression is checked against it, and
only in the case they match, the exception is
caught.
C++ exceptions

Based on
runtime stack
C++ exceptions
• We can chain multiple try {
handlers (catch expressions), // code here
each one with a different
parameter type. } catch (int param) {
– Only the handler that matches cout << "int exception";
its type with the argument } catch (char param) {
specified in the throw statement
is executed. cout << "char
• catch (...) will catch any exception";
exception no matter what the } catch (...) {
type of the throw exception is. cout << "default
– This can be used as a default exception"; }
handler that catches all
exceptions not caught by other
handlers if it is specified at last
exceptions
• C does not have exceptions but non-local jumps
possible via setjmp(), longjmp() library calls.
• C++ and Java: try {...} catch(...) {...}
• Each try-catch block introduces a non-local jump point.
• try block is executed and whenever a throw expr
command is called in any functions called (even
indirectly) inside try block execution jumps to the
catch() part.
• try-catch blocks can be nested.
• Execution jumps to closes catch block with a matching
type in the parameters with the thrown expression.

You might also like