Throw Statement in Java

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:01:00

Throw statement in Java

The throw statement in Java is used to explicitly raise an exception within a function, and it applies to both checked and unchecked exceptions within the method. It allows for the controlled triggering of exceptions during program execution, making it possible to properly handle uncommon scenarios.

Throws keyword in Java

The throws keyword in Java is used to declare an exception in a program, mostly for Checked Exceptions, allowing methods to declare numerous exceptions that may be thrown during execution. This provides a clear indicator of potential uncommon occurrences and allows for appropriate management in calling code.

Java Custom Exception

The term "custom exception" refers to the creation of one's exception to customize an exception to meet specific demands. The custom exceptions derive from the Exception class.

Need for Java Custom Exceptions

  • To classify the exceptions depending on the many types of mistakes in your project.
  • To allow for exception handling at the application level.

Build a Java Custom Exception

To build a custom exception, you have to create a class that inherits from the Exception class.

Rules For Creating Custom Exceptions

  • Every exception must be a Throwable child.
  • To write a checked exception that is enforced automatically by the Handle or Declare Rule, you must extend the Exception class.
  • To write a runtime exception, you must extend the RuntimeException class.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this