Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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 ). 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

closed as unclear what you're asking by gnat, durron597, GlenH7, Snowman, MichaelT Aug 13 at 0:27

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

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
"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

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