Throws exception is the method followed by means of throwing an exception to the superior
The exception in try{} is caught by an outside catch
Throw exception is throw new Exception ("exception");
Use the following
12345678910 |
public
static
void
main(String args[]){
try
{
A();
}
catch
(Exception e){
System.out.println(e.getmessage());
}
}
public
static
void
A()
throws
Exception{
throw new
Exception(
"A方法抛出了一个异常"
);
}
|
(1) If you do not want to write specific code to catch the exception, you can use the form of throws Exception,
Throws the exception again, to the JVM (the Java virtual machine) to Capture. This is a more convenient way.
(2) If you want to write code that handles exceptions, you can use the form of Try{}catch () {} to capture,
Once the program has an exception, it will be executed according to the code written by your catch{} Block.
Throws used on methods to declare that the method has an exception that can be thrown
Try{}: catch exception
Catch: Handling Exceptions
Java throws Exception, try, catch