1 /*2 Throw: If there is an exception, we can throw the exception, this time the exception should be thrown object. 3 4 the difference between throws and throw (interview question)5 throws6 used after the method declaration, followed by the exception class name7 can be separated by a comma with multiple exception class names8 represents a thrown exception that is handled by the caller of the method9 throws represents a possibility of an exception that does not necessarily occurTen Throw One used in the body of a method, followed by the name of the exception object A only one exception object name can be thrown - represents a thrown exception, handled by a statement in the method body - Throw throws an exception, and execution throws must throw some kind of exception. the - Exception Handling principle : If the function can be handled internally, with a try, if not handled, referred to the caller, which is used throws - difference: - subsequent programs need to continue to run on the try + subsequent programs do not need to continue to run throws - Example: + If you have a cold, just take some medicine for yourself, try . A after a few days of medicine did not have a good result of the throws, then we have to go to the hospital at If the hospital doesn't have a cure, it becomes an error. - - */ - Public classExceptiondemo { - Public Static voidMain (string[] args) { - //method (); in - Try { to method2 (); +}Catch(Exception e) { - e.printstacktrace (); the } * } $ Panax Notoginseng Public Static voidmethod () { - intA = 10; the intb = 0; + if(b = = 0) { A Throw Newarithmeticexception (); the}Else { +System.out.println (A/b); - } $ } $ - Public Static voidMETHOD2 ()throwsException { - intA = 10; the intb = 0; - if(b = = 0) {Wuyi Throw NewException (); the}Else { -System.out.println (A/b); Wu } - } About}
The difference between Java 19-7 throw and throws