Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Introduction to Programming Languages/Throwing exceptions

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

The throw Statement Not all exceptions are created automatically. We can also throw exceptions manually.throw new NullPointerException(); Creating Custom Exceptions public class OutOfGas extends Exception { }Now we can do:throw new OutOfGas(); Why Create Custom Exceptions? public class OutOfGas extends Exception { public OutOfGas(String details) { super(details); } }Usage:throw new OutOfGas("You ran out of fuel."); getMessage() Inside catch:catch (OutOfGas e) { System.out.println(e.getMessage()); }Output:You ran out of fuel. The Role of super() super(details);calls the parent constructor. This initializes: the exception message; inherited fields.