7.2 Throws and throw keyword
7.2.1 Throws Keywords
You can use the throws keyword declaration when defining a method, using the method declared by throws to identify that the method does not handle the exception, and that it is handled at the call of the method.
Example: using the throws keyword
Example: Handling Exceptions
Tips:
The throws keyword can also be used in the main method, but the main method is the starting point of the program, so when the main method throws an exception upwards, the exception can only be thrown to the JVM for processing. The main method is the starting point for everything, and if you use throws in the Main method, then the problem is definitely left to the JVM for processing, which will cause the program to break and it is not recommended to use throws on the main method.
7.2.2 Throw keyword
Unlike the throws keyword, you can use the Throw keyword to artificially throw an exception, throwing an instantiated object that throws the exception class directly.
Example: Throwing an exception
Package Limethrowable._7_2_2.throw_; Public class ThrowDemo01 { publicstaticvoid main (string[] args) { Try{ thrownew Exception ("Lime throw a Exception"); Catch (Exception e) { e.printstacktrace (); }}}
Console:
Throw a exception at Limethrowable._7_2_2.throw_. Throwdemo01.main (Throwdemo01.java:7)
La La la
7.2.3 Example--application of throw and throws
Java--Exception capture and processing--throws and throw keyword