The doctrine of necessity is a legal principle that allows for the justification of actions that would otherwise be unlawful when those actions are taken to prevent a greater harm or to protect the public interest. It often comes into play in common law systems, where legal precedents can provide a framework for understanding how this doctrine applies in various scenarios, especially in emergency situations where immediate action is required.