Knowledge is defined as the state of being aware of consequences of the harm or injury incurred whereas intention conveys the state of mind of committing an act with the purpose and intent to cause such harm or injury.
Herein knowledge and intention could be differentiated by this simple illustration:
X is aware of the fact that fire may cause serious injury that means he is capable of understanding the fact and possess the knowledge of the consequences that would occur still he throws burning matchsticks on the passer-by, this statement directs that with the intent to cause harm he injures other people. This clarifies that the knowledge and intention both are present in the given scenario.
Section 81 of the Indian Penal Code, 1860 states that the act likely to cause harm without criminal intent, and to prevent other harm which means the knowledge is available that it is likely to cause harm but the intention to do the act without criminal and guilty mind is necessary.
The act should either be done in good faith or under sheer necessity where the choice is to commit certain harm and to decide which one is the lesser harm to be done to avoid the greater harm that could not otherwise be avoided. This is the only way to avoid criminal responsibility in such cases.
The case where the captain of a ship in order to prevent the ship from wrecking into the boat with eight people on board on the right side and on another boat two people on board on the left side decides to crash into the boat with fewer people would be considered as a classic example of Section 81 under sheer necessity and in good faith to prevent the greater harm from happening by committing a lesser evil.
Here the knowledge of harming the two people on the other boat was clear to the captain, but in order to protect the lives of the other eight was an essential (good faith without criminal intent), so the exception to necessity can be applied here.
Though the delineate line between knowledge and intention is no doubt quite thin, but still it is not that difficult to common reasoning and understanding to distinguish the two.