Difference and connection between throw and throws

Source: Internet
Author: User

I suddenly found that today's poetry is booming, and it seems that I have to write something again.

Throw and throws are really not unfamiliar to Java programmers. But as soon as contestants like me mention their differences and connections, they will be covered...

For the sake of the future, study it today ~~

If there are any deficiencies, please criticize and correct them...

1) throws

The throws keyword is generally used to throw an exception during compilation. If an exception occurs during compilation, capture the exception (try {} catch {} finally {})

Or throws ). Otherwise, compilation fails.

Let's talk about the differences between the two.

For try {} catch {} finally {}, the corresponding exception has been caught in the program. Related exceptions are also handled in the corresponding catch and will not be thrown up. Upper Layer

The caller does not need to perform any processing.

For throws, the current program will not handle any exceptions. If an exception occurs, it will only throw and will not be processed. The corresponding processing needs to be handled by the upper layer. Corresponding

The upper-layer caller must handle this exception or continue to throw the corresponding exception.

To put it bluntly, take the company as an example:

The customer raised a bug for the developer. If the developer can handle (try {} catch {} finally {}), the developer will handle it by himself and will not tell the project

. However, if the development cannot be processed, the development will find the project manager (Development throws). If the project manager can handle the problem (try {} catch {} finally {}). If

If you are not sure, contact the General Manager (the throws of the Project Manager). The problem will be handled in the end. (do not dare not handle it. The customer is God ~~ Haha)

The following is a simple example of throws:

Package COM. yonyou. test; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception;/*** test class ** @ author xiaohao * @ creation date 2015-3-2 */public class test {public static void main (string [] ARGs) throws ioexception {file = new file ("C: \ text.txt"); fileinputstream FD = new fileinputstream (File); byte [] B = new byte [10]; while (FCM. read (B )! =-1) {system. Out. println (new string (B ));}}}

 

  

 

2) Throw

Throw is generally used to throw an exception object, but not many are used. For more information, see the following example:

Package COM. yonyou. test; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception;/*** test class ** @ author xiaohao * @ creation date 2015-3-2 */public class test {public static void main (string [] ARGs) throws myexception {file = new file ("C: \ text.txt"); fileinputstream FCM; try {FD = new fileinputstream (File ); byte [] B = new byte [10]; while (FCM. read (B )! =-1) {system. out. println (new string (B) ;}} catch (ioexception e) {// handle caught exceptions and throw an exception object throw new myexception ("Dragon does not Yin, hu do not declare ") ;}}/ *** custom exception * @ author Xiao Hao * @ creation date */class myexception extends exception {Private Static final long serialversionuid = 1l; public myexception (string info) {super (Info );}}

The running result is:

Exception in thread "Main" com. yonyou. Test. myexception: Dragon, tiger, and custom exception object
At com. yonyou. Test. Test. Main (test. Java: 27)

Okay. Let's go here today ~~ See you

 

Difference and connection between throw and throws

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.