Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been searching for a definition for handler. basics i've understood that "A handler is a piece of code that is called when something happens, and usually takes some action, like generating a response." - (from http://stackoverflow.com/questions/3246200/what-is-an-handler ). But that can be a trigger or a callback. Also in specific an event handler on a low-level , often works by polling a device and waiting for a hardware response.

So, what is the specific role of a handler ( that makes it unique from a trigger or a callback or any other such function ). Do all handlers have similar role ( event handler, file handler , exception handler, error handler )...

share|improve this question
1  
The canonical implementation of low level handlers does not involve device polling. Usually they respond to software events generated by lower level interrupt service routines, which are triggered by hardware interrupts. –  mouviciel Nov 5 '13 at 8:54
    
In fact, polling is never done by any handler. Handlers are reactive, polling is active. A generic loop may actively poll and then call a handler, because in that situation the handler is still reactive. –  MSalters Nov 5 '13 at 12:05
1  
"A handler is a piece of code that is called when something happens, and usually takes some action, like generating a response." That describes most code. –  Brandon Nov 6 '13 at 2:07

2 Answers 2

up vote 0 down vote accepted

"Handler" is a word used to refer to any piece of code that is invoked conditionally to deal with a specific case in any run-time situation whatsoever.

So whereas on the one hand we have interrupt handlers and exception handlers, when we look at the broad usage, it comes to light that it is much more general: handlers are not only for exceptional situations.

In the scanner for a programming language, a rule which recognizes and consumes /* ... */ comments might be called a handler for comments, at least by someone. Someone reading the language specification and looking at the code might ask the question, "I see this language has comments. Where in this lexical analyzer is that input handled?"

To handle simply means to support for a situation or datum which occur at run-time, and a handler is some unit of code where that requirement is implemented.

share|improve this answer

There is no specific task that a handler does. The definitions you have found really do capture the essence: a routine that is called in reaction to an external event (mouse click by the user, arrival of an email message, exhaustion of physical memory) is a handler. A routine that is called because the program structure says so (e.g. the second pass in a two-pass compiler) is simply a routine, or part of an algorithm.

What the code does is not relevant for this distinction. Obviously a mouse handler will usually move the graphical representation of the mouse pointer across the screen, and a mail handler will probably move the message to some folder, or discard it altogether. But you could also have a manual reorganisation function that moves mail messages as part of a predefined algorithm, or a graphics demo that moves your mouse pointer because the program simulates my desktop on your screen rather than because you moved your mouse. In that case the same code would simply be code, and not a "handler".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.