Java niu Jiao Jian "007": Error in Java can be catch

Source: Internet
Author: User
Tags throwable

Java niu Jiao Jian "007": Error in Java can be catch

There are many friends on the internet who say that error in Java cannot be caught, and that the type of error defined in Java is difficult to test, so be sure to do so.

But maybe everyone has noticed that we often see code like this

[Java]View PlainCopy
    1. try{
    2. ...
    3. }catch (Throwable ex) {
    4. ...
    5. }

Where catch is directly captured is a Throwable class, open the inheritance relationship to see, exception and error two classes are also inherited from the Throwable class, then, that is, error should be able to be captured, here is an example to prove the guess:

[Java]View PlainCopy
  1. Package net.moon.demo.errorcatch;
  2. Public class Demo {
  3. /** 
  4. * @param args
  5. */
  6. public static void Main (string[] args) {
  7. //TODO auto-generated method stub
  8. try {
  9. throw New Myerror ("My Error");
  10. } catch (Myerror e) {
  11. System.out.println (E.getmessage ());
  12. }
  13. }
  14. }
  15. Class Myerror extends Error {
  16. /** 
  17. *
  18. */
  19. private Static final long serialversionuid = 1L;
  20. Public Myerror () {
  21. super ();
  22. //TODO auto-generated constructor stub
  23. }
  24. Public myerror (String message, throwable cause) {
  25. super (message, cause);
  26. //TODO auto-generated constructor stub
  27. }
  28. Public myerror (String message) {
  29. super (message);
  30. //TODO auto-generated constructor stub
  31. }
  32. Public Myerror (Throwable cause) {
  33. super (cause);
  34. //TODO auto-generated constructor stub
  35. }
  36. }

Execute the above code, as the previous guess, error is can be captured, run the code result is:

[XHTML]View PlainCopy
    1. My Error

Here is a small example to verify the capture of the error.

Copy CodeThe code is as follows:
public class Testcatcherror extends error{

Private static final long serialversionuid = -351488225420878020l;

Public Testcatcherror () {
Super ();
}

Public Testcatcherror (String msg) {
Super (MSG);
}

public static void Main (string[] args) {
try {
throw new Testcatcherror ("Test catch Error");
} catch (Throwable t) {
SYSTEM.OUT.PRINTLN ("step in the Catch ~");
T.printstacktrace ();
}
}
}

Operation Result:

Copy CodeThe code is as follows:
Step in the Catch ~
Testcatcherror:test Catch Error
At Testcatcherror.main (testcatcherror.java:23)

Java niu Jiao Jian "007": Error in Java can be catch

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.