Featured

    Featured Posts

Exception and Exception handlers in Programming language.



        During execution of a program, events or conditions often occur that might be considered exceptional.  Rather than continue with normal program execution, a subprogram needs to be called to perform some special processing, including being able to handle the following situations.
  1. Error conditions : To process an error such as an arithmetic operation overflow or reference to an array element with a subscript out of bounds.
  2. Unpredictable conditions That arise during normal program execution such as production of special output headings at the end of a pointer page or an end-of-file indicator on an input file.
  3. Tracing and monitoring during program testing.  Such as printing trace output during program testing when the value of a variable changes.
Although it is usually possible to insert an explicit test in the program to test for these exceptions, such extra statements can obscure the programs basic structure.  It is simpler that to relax the requirement that subprogram must be invoked be explicit calls and provide a way that a subprogram may be invoked when a particular condition or event occurs.  Such a condition or event is usually termed an exception or a signal and the subprogram that performs the special processing is termed as exception handler.  When an exception occurs, interrupting program execution, and transferring control to the exception handler is called raising the exception.

Exception Handlers :-> Because an exception handler is invoked without an explicit call;  it ordinarily does not require a name or parameters.  The definition of an exception handler typically contains only :
  1. A set of declarations of local variables &
  2. A sequence of executable statements.
Raising an Exception :->
             Exception are processed in C++ and java  via try clause.  C++ raises an exception by throwing the exception, and the exception is handled by catching the exception.

try {
          statement1 ;
          statement2 ;
          -----------
if Bad condition ( throw Exception Name } ;
}
Catch ExceptionName{ // Do something for Exception }
    } // End of Exception.
}
Syntax of try and catch block.


# Propagating an exception :->  Generally an error is handled with in the same program but when an exception is not handled by the same program by the some other program is known as propagating an error.
       One important effect of this rule is that it allows a subprogram to remain as a programmer defined abstract operation even in processing exceptions.
       If the exception us handled within the subprogram then the subprogram returns in the normal way, and the caller is never aware that an exception has been raised.

Assertions :->  It is almost like an exception.  An assertion is simply a statement implying a relation among data objects in a program, as in the C++ statement
#include<assert.h>
---------
---------
assert( x > y+1) ;
It will work like if ( x > y+1 ) ;  /* Print error message */

# Coroutine :->  A program that return to their calling program before completion of execution such subprogram are termed coroutines.
       When a co-routine receives control form another subprogram, it executes partially and then is suspended when it returns control.  At later point, the calling program may resume execution of the coroutine from the point at which execution was previously suspended.
        Suppose if A calls subprogram B as a coroutine, B executes awhile and return control to A, just as any ordinary subprogram would do.  When A again passes control to B via a resume B, B again executes awhile and returns control to A, just as an ordinary subprogram.  Thus, to A, B appears as an ordinary subprogram.  However, the situation is similar when viewed from subprogram B.  B in the middle of the execution,resumes execution of A.  A executes a while and return control to B.  B continues execution a while and returns control to A.  A executes awhile and returns control to B.  From subprogram B, A appears much like an ordinary subprogram.  The name coroutine derives from this symmetry rather than having a parent-child or caller-callee relationship between the two subprograms, the two subprograms appear more as equals - two subprograms swapping control back and forth as each executes, with neither clearly controlling the other.
fig-----

Implementation :- The resume instractions that transfer control between coroutines must have to specify the resumption of some particular activation of the coroutines.  If there are multiple recursive activation's of a coroutine exists at a time.
          The restriction over a co-routine is that at most one activation of  given co-routine exists at a time.
          A location is now need to be stored with a resume point, is reserved in the activation record to save the old ip value of the CIP.  The return command used to save the value of ip i.e a co-routines is used to store the ip value for itself.
       The resume instruction in co-routine involves two steps :->
  1. The current value of the CIP is saved in the resume point of the activation record for A.
  2. The ip value in the resume point location of B is fetched from B's activation record and assigned to the CIP to effect the transfer of control to the proper instruction in B.
  1. Java threads and Interrupts in Java programming language
  2. Abstract classes and Generic function in Java
  3. Callback functions in Java Programming language
  4. Constructors in programming language
  5. Static components and constants concept in programming language
  6. Data encapsulation in programming language.
  7. Java threads and Interrupts in Java programming language


Post a Comment

www.CodeNirvana.in

www.posthatke.com. Powered by Blogger.

Google+ Followers

Copyright © www.posthatke.com | Blogger Templates | Designed By posthatke.com