Throw is used to throw an exception directly after classes and methods are throws.
Throws is placed behind the class and method, and then you want to call the class and method, throw the class or method exception directly.
Throw is a single statement throws an exception!
Throw is throwing a
Although the two seem to have only one s difference, but the effect is completely different/////java handling unusual way///////////////////////////////in Java code, if an exception occurs, the JVM throws an exception object that causes the program code to break.
The JVM is doing this by creating an exception object and then throwing it, for example: int i= 1; int j = 0; int res = 0; res = i/j;//except 0 error System.out.println (res); These 5 lines of code run to the fourth sentence will be interrupted because the JVM throws an exception////throw action/////////////////////////////////////////manually throws an exception but sometimes some errors are not mistaken in the JVM, such as int
Age = 0;
Age =-100;
System.out.println (age);
The normal shape of the variable assignment, but in our eyes seems to be abnormal, whose age will be negative.
So we need to throw ourselves an exception manually, which is the role of throw int age = 0;
Age =-100;
if (age<0) {Exception e = new Exception ();//Create exception object throw e;//throw Exception} System.out.println (age); Throws action///////////////////////////////////declaration method may avoid exceptions are thrown out, it is necessary to do processing, so there are try-catch in Java but sometimes a method produces an exception, But do not know how to deal with it, then put it regardless, when the exception is thrown will interrupt the method, and the exception is thrown to the caller of this method. This is a bit like a problem that the subordinates can't handle. This is called avoidance exceptions but this makes calling this method risky because no one knows when this method throws an exception to the caller, so when you define the method, It is necessary to use throws in the method header to declare the exception void fun () throws Ioexception,sqlexception {...} that this method may evade, which means that the fun method may throw two exceptions. Then you'll be prepared to call fun, such as try {Fun ();} catch (IOException e) {}catch (sqlexceptIon e) {}