Irony is an important technique that writers often use to engage a reader's attention and often to help develop deeper levels of meaning in a text. It works to 'shape' meaning in often subtle ways to help create 'layers of meaning'. It is a very common 'literary device' that writers employ so you'll find it in many kinds of texts - not just literary fiction and poetry. Always be on the lookout for it as it's often subtle and thus will lead to high marks.
Irony is, at heart, about "positives" and "negatives". It occurs when a writer in non-fiction, or a narrator or character in fiction - state a positive or a negative, but mean a character in the text, or the reader... to understand the opposite. If you look out at pouring rain and state a positive, "It's fine weather we're having!", your friend - who shares your understanding of the need for sunny weather - then looks out of the window and interprets correctly that you meant a negative: that your reaction to the weather was one of disappointment but you expressed it - ironically - as pleasure.
Thus, when using irony a writer depends upon some form of 'shared understanding' between him or herself and the reader. For the irony to 'work' both writer and reader need to recognise that what is written is not quite what is meant: it's as if there is a 'gap' existing between the surface meaning of the words used and the deeper meaning intended, a gap that once worked out can lead to a pleasurable sense of satisfaction as well as a deeper connection with the writer or the text.
At its most obvious, and some would say crude, irony is sarcasm; however, in this basic use of irony, the intended meaning is always quite obviously indicated because of the use of a snide or cutting tone of voice, one that immediately suggests that the exact opposite of what is said has been meant, e.g. a compliment given is meant as a mocking criticism. Literary irony, however, is much more subtle and less evident; indeed, it requires a close awareness of the text sometimes to appreciate its presence. This is why noticing it and discussing it generally gains high marks in essays and exams. In literary uses, irony is always much more witty, too - even though it often retains a varying degree of 'edginess' or criticism at times.
Why do writers use irony so subtly? Well - they know that readers do not like to be lectured: irony allows meanings to surface unobtrusively, leaving the reader to ponder on what is meant thus increasing the level of engagement with the text. Irony creates a great closeness to a text and a feeling of satisfaction when the irony is recognised and understood - a little like a shared understanding between the writer and the reader - a kind of knowing wink!
Here are some examples of how irony can operate in a text:
(1) A character can say something that the reader recognises
is a mistake - here the 'ironic gap' is between words and
(2) A character may say something, the real meaning or implication of which is quite different from what the character supposes to be the case. Here the ironic gap is between words and meaning.
(3) A character can expect certain events to happen or can set out to achieve something, but the reader (or in the case of a play, the audience) has knowledge the character does not and so knows that things won't work out as expected. Here the gap is between intention and outcome. This is sometimes called DRAMATIC IRONY.
(4) A character can interpret the world in one way, but the reader will see that this interpretation is wrong. Here the gap is between appearance and reality.
DRAMATIC IRONY is particularly important if you are studying a play. As suggested above, it occurs when the audience knows more about a character than the character on stage does. Hence, in a childhood pantomime, you want to call to 'Jack' that 'the giant' is behind him - and you do! But in a play (or on TV), you simply cannot call out - yet the involvement you feel with the play is intense at that moment: you almost become a conspirator to the action.