PLC Session 11

Session 11: Exception Handling and Event Handling

 

A. Introduction to Exception Handling

With Exception Handling ,the programs are allowed to trap some exceptions, thereby providing the possibility of fixing the problem and continuing .

 

If there isn’t any exception handling , then the programs will stop working when it encountered an error and show some display messages .

 

What is an exception ?

An exception (or exceptional event) is a problem that arises during the execution of a program , or an unplanned event that occurs while a program is executing and disrupts the flow of its instructions.

 

A exception in C++ language is a response to an exceptional circumstance that arises while a program is running.

 

Exception can happen for many different reasons . For example :

  • A user has entered an invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.

 

Exceptions are of two types:

 

  1. Synchronous Exceptions :

Errors that occur due to incorrect input data or incorrect handling of array indices (“out of range index”), memory overflow are known as Synchronous Exceptions.

 

  1. Asynchronous Exceptions :

The errors that are caused by events that are beyond control of the program are Asynchronous Exceptions. E.g. Keyboard interrupts.

 

 

So what is an exception handling exactly?

Exception handling is a construct designed to handle the occurrence of exceptions, that is special conditions that changes the normal flow of program execution.

Exception handling allows the programmer to manage runtime errors in an orderly fashion. Using exception handling, the program can automatically invoke an error handling routine when an error occurs.

 

It has been said that there are two types of exception , but unfortunately C++ exception handling mechanism can takes care of only Synchronous Exceptions.

 

Exception handling consists in transferring control from the place where exception happened to the special functions (commands) called handlers. So ,Exception Handler is code that stipulates what a program will do when an anomalous event disrupts the normal flow of that program’s instructions.

 

 

How if the programming language can’t handle the exception ?

Because our groups has choosen to talk about C++ language then we will talk about C++ language .

Based on accu.org , there are several ways to handle this .

  • Return Codes is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task.

The main problems with the return codes approach are:

 

  • They are unusable for constructors, destructors and overloaded operators.
  • They are difficult to propagate to the place where the event can be handled consistently for the whole application. In our example the problem occurs inside an application function, and it must be handled by the application, but the framework, which doesn’t know what kind of problems might occur, must itself propagate the error.
  • Programmers ignore them.

 

  • .Deferred Error Handling ,

Probably , it works this way :

  • An object internal error flag is set, if a problem occurs.
  • This flag can be checked by clients using a special member function (e.g. overloaded !-operator).
  • All member functions of the class check the flag and act respectively (probably by doing nothing).

 

The advantage of deferred error handling is the invisibility to the non-interested parts of the application. In our framework example the errors are completely transparent.

 

A disadvantage of deferred error handling is the long time, which might pass between the detection and the handling of an error.

 

  • Error Stack

The error stack mechanism is essentially the same mechanism as standard C++ exception handling. You typically cannot do anything useful as long as there are unhandled exceptions. Your only actions when you have hanging exceptions are in most cases clean-up actions and manual stack unwinding (returning from the function). But, you have no hidden stack unwinding without your control, and you can have more than one exception at the same time on the stack, so you don’t have artificial (i.e. coming from the C++ environment and not from the problem domain) problems with exceptional events in destructors.

 

  • Safe Termination

This is the best approach for dealing with programming errors. Unfulfilled pre-conditions, such as “index out of range” or “pop from empty stack”.

 

 

B. Exception Handling (C++)

  1. Built-in Exception

C++ provides a range of built in exceptions. The base class fo all exceptions classes is exception.

 

Exceptions derived directly from exception class:

 bad_alloc  Happens when there is a failure of memory allocation
 bad_cast  Is thrown when dynamic_cast is used incorrect
 bad_exception  Exception that is thrown by unexpected handler
 bad_function_call  Thrown when an empty (not implemented) function is called
 bad_typeid  Thrown by typeid function
 bad_weak_ptr  Exception that can be thrown by shared_ptr class constructor
 ios_base::failure  Base class for all the stream exceptions
 logic_error  Base class for some logic error exceptions
 runtime_error  Base class for some runtime error exceptions

 

 

Advantages of Built-in Exception Handling :

  • Error detection code is tedious to write and it clutters the program.
  • Exception handling encourages programmers to consider many different possible errors.
  • Exception propagation allows a high level of reuse of exception handling code

 

 

  1. C++ Exception Handler

C++ exception handling is built upon three keywords: try, catch, and throw.

 

  • The catch Function

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. It is an overloaded name, so the formal parameter of each must be unique

 

 

 

  • Throw

Throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

 

Exceptions are all raised explicitly by the statement: throw [expression]; à The brackets are metasymbols.

 

A throw  without an operand can only appear in a handler; when it appears, it simply re-raises the exception, which is then handled elsewhere

 

  • Try

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.

 

 

  1. Unhandled Exceptions

An unhandled exception is propagated to the caller of the function in which it is raised.

An unhandled exception shows that your code is running in a situation which you did not anticipate, and there is something about how your code runs that you do not understand.

This propagation continues to the main function. If no handler is found, the default handler is called.

 

  1. Continuation

Continuations are useful things. They provide a nice way to manually handle control flow.

In a language which exceptions, every expression can either do one of two things :

  • Continue the rest of the program normally
  • Throw an exception and run an alternative program, the exception handler

 

And so , we don’t just have a single continuation.

 

C. Event Handling

  1. Event

An event is a signal that informs an application that something important has occurred.

User actions such as Key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications can be called as events.

 

  1. Event Handler

Event handling is the receipt of an event at some event handler from an event producer and subsequent processes. The processes involved in event handling include:

  • Identifying where an event should be forwarded.
  • Making the forward.
  • Receiving the forwarded event.
  • Taking some kind of appropriate action in response, such as writing to a log, sending an error or recovery routine or sending a message.
  • The event handler may ultimately forward the event to an event consumer.

 

Event handlers are procedures that are called when a corresponding event occurs. You can use any valid subroutine with a matching signature as an event handler. You cannot use a function as an event handler, however, because it cannot return a value to the event source.

 

  1. Event Handling in C++
  • Declaring Events

In an event source class, use the __event keyword on a method declaration to declare the method as an event. Make sure to declare the method, but do not define it; to do so will generate a compiler error, because the compiler defines the method implicitly when it is made into an event. Native events can be methods with zero or more parameters. The return type can be void or any integral type.

 

  • Defining Event Handlers

In an event receiver class, you define event handlers, which are methods with signatures (return types, calling conventions, and arguments) that match the event that they will handle.

 

  • Hooking Event Handlers to Events

Also in an event receiver class, you use the intrinsic function __hook to associate events with event handlers and __unhook to dissociate events from event handlers. You can hook several events to an event handler, or several event handlers to an event.

 

  • Firing Events

To fire an event, simply call the method declared as an event in the event source class. If handlers have been hooked to the event, the handlers will be called.

Source(s) :

http://whatis.techtarget.com/definition/exception

https://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm

https://www.tutorialspoint.com/java/java_exceptions.htm

http://www.careerride.com/C++-exception-handling-concept.aspx

https://en.wikibooks.org/wiki/C%2B%2B_Programming/Exception_Handling

http://www.theserverside.com/definition/exception-handler

https://accu.org/index.php/journals/546

https://en.wikipedia.org/wiki/Exit_status

https://www.tutorialcup.com/cplusplus/exception-handling.htm

http://geekswithblogs.net/simonc/archive/2013/06/03/why-unhandled-exceptions-are-useful.aspx

http://jozefg.bitbucket.org/posts/2014-04-14-either-and-conts.html

https://msdn.microsoft.com/en-us/library/2z7x8ys3(v=vs.90).aspx

https://www.tutorialspoint.com/csharp/csharp_events.htm

http://www.webopedia.com/TERM/E/event_handler.html

https://msdn.microsoft.com/en-us/library/ee2k0a7d.aspx

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.