Reproduced Exception capture Order in Java (multiple catch)

Source: Internet
Author: User

http://blog.sina.com.cn/s/blog_6b022bc60101cdbv.html[reprint]java in the Order of exception capture (multiple catch)(2012-11-05 09:47:28) reproduced
Tags: reprint Category: Reprint
Original Address:Exception capture Order in Java (multiple catch) Leesajava 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: In the write exception processing, it is important to put the scope of the exception in the front, the scope of large in the back, exception the root of the exception must be just in the last catch, if placed in front or in the middle, any exception will and exception match, will be reported to have captured ... The error of the exception.

Change from: Javaeye blog

Reproduced Exception capture Order in Java (multiple catch)

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.