The difference between Java 19-7 throw and throws

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.