There are multiple exceptions in Java, how to determine the capture order (multiple catch), first execution from top to bottom, determine the size of the exception, if the inclusion of an exception, enter the catch, and then no longer execute

Source: Internet
Author: User

Java code
  1. Import java.io.IOException;
  2. public class Exceptiontrycatchtest {
  3. public void DoSomething () throws IOException{
  4. System.out.println ("Do somthing");
  5. }
  6. public static void Main (string[] args) {
  7. Exceptiontrycatchtest et = new exceptiontrycatchtest ();
  8. try {
  9. Et.dosomething ();
  10. } catch (Exception e) {
  11. //
  12. } catch (IOException e) {
  13. //
  14. }
  15. }
  16. }

Question: Can the above program be compiled and passed? Why?


Solution: cannot compile through.  Because the compile time will be error: has caught the exception java.io.IOException. catch (IOException E) There is an error in this sentence.
Analysis: for try. In the form of catch catch exceptions, there can be more than one catch for an exception capture. In the case of a try inside the exception, he will be based on the occurrence of the exception and catch inside the match (how to match, according to catch block from top to bottom), when it matches a catch block, he went directly into the catch block, followed by a catch block, It does not do any processing, jump directly past, all ignored. If there is finally, go to the finally inside to continue execution. In other words, if there is a matching catch, it ignores all the catch behind the catch. For us this method, the throw is IOException, when executed etct.dosomething (); When, may throw IOException, one but throws IOException, it first enters into catch (Exception e) {} inside , first and Exception match, because Oexceptionextends Exception, according to the principle of polymorphism, IOException is matching Exception, so the program will go into catch (Exception e) {} inside, After entering the first catch, the catch will not be executed, so catch (IOException e) {} will never execute, giving us the previous error: The exception java.io.IOException has been caught.

Summary: When writing exception handling, be sure to put the exception range of small in front , the scope of large in the back, exception the root of this exception must be just in the last catch inside, if placed in front or in the middle, Any exception will match the exception, it will be reported to have been captured ... The error of the exception.

There are multiple exceptions in Java, how to determine the capture order (multiple catch), first execution from top to bottom, determine the size of the exception, if it contains an exception, enter the catch, and then no longer execute

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.