Where an accused committed a criminal offence in order to avoid certain consequences where they would have been in immediate irreparable harm, or in order to protect someone else who they were bound to protect, the defence of necessity will be available. It is however a rarely used defence as the circumstances in which it would likely be viable very infrequently arise. The way a person reacts needs to be proportionate to the level of harm or peril they are facing: R v Loughnan [1981] VR 443.
Whether or not this defence is made out depends entirely on the facts of the matter. It is a very difficult defence to make out successfully and requires a great deal of work and preparation. Essentially, the act committed by the person needs to be weighted up against the harm the person would have experienced had they not acted in that manner.
Who has to prove what?
Where an accused is raising the defence of necessity, the burden is on the accused to call evidence to prove that they were acting out of necessity. If the accused is able to successfully do so, then the prosecution must negate the defence beyond reasonable doubt.