Complete this example and say the handling mechanism for exceptions in Java.

Source: Internet
Author: User
Tags throw exception

There is a class of ClassA, there is a class of CLASSB, there is a method B in ClassB, this method throws an exception, in the ClassA class there is a method A, call B in this method, and then throw an exception. In a client that has a class of TESTC, there is a method for C, please catch the exception information in this method. To complete this example, describe the handling mechanism for exceptions in Java.

[Java]View Plaincopy
  1. Package Com.itheima;
  2. Import java.io.IOException;
  3. /**
  4. * Question 6th: There is a class ClassA, there is a class of CLASSB, in ClassB there is a method B,
  5. * This method throws an exception, there is a method a in the ClassA class, call B in this method, and throw an exception.
  6. * In the client there is a class of TESTC, there is a method for C, please catch the exception information in this method.
  7. * To complete This example, please describe the handling mechanism for exceptions in Java.
  8. *
  9. Exceptions in *java are divided into "Runtime exceptions" and "compile-time exceptions"
  10. * Run-time exception: is the program in the process of running the exception generated, this is not necessary to deal with, we need to fix
  11. * Compile-time exception: is the program in the process of compiling the exception generated, this we have to deal with
  12. * In Java programs, there are two kinds of exception handling mechanism, one is "throw exception", one is "catch exception", one is "default out"
  13. * Default processing, he will put the exception name, reason, location and other information output in the console, but the program will not continue to execute the
  14. * Throw exception: Use the keyword throws throw
  15. * Throws is used to throw an exception when declaring a method, only an exception
  16. * Throw is used to throw an actual exception, throw a specific exception object, can be more than one exception.
  17. * Catch Exception: try....catch...finally
  18. * try{code block}
  19. * The code block that is contained in the middle is the code that may appear to be abnormal.
  20. * catch{code block}
  21. * This code block is the code to handle the exception
  22. * finally{code block}
  23. * This is a block of code that needs to be executed after exception handling. This block of code is always executed to release resources.
  24. * Note: Try cannot be used alone, it must be used with one of the catch or finally two
  25. */
  26. Public class Test6 {
  27. Public static void Main (string[] args) throws Exception {
  28. Create TC Object
  29. TESTC TC = new TESTC ();
  30. Use TC to invoke the C method and run this class
  31. TC.C ();
  32. A = new A ();
  33. A.A ();
  34. }
  35. }
  36. Class A
  37. Class a{
  38. Create a method, create a B object in the A method, call the B method through the B object, because the B object throws an exception, and you also throw an exception in the A method
  39. public Void A () throws exception{
  40. b b = new B ();
  41. B.B ();
  42. }
  43. }
  44. Class B
  45. Class B {
  46. Create a B method and throw an exception
  47. public void B () throws exception{
  48. System.out.println ("I am a B method in class B, I throw an exception!")  ");
  49. }
  50. }
  51. Client class
  52. Class testc{
  53. public void C () {
  54. Create a object of a
  55. A = new A ();
  56. Call the A method in the A object, at this point because an exception is thrown in the A method, so this must be handled, using Try...catch processing
  57. try {
  58. A.A ();
  59. } catch (Exception e) {
  60. //TODO auto-generated catch block
  61. System.out.println ("I am the catch exception method," +e.tostring ());
  62. }
  63. }
  64. }

Complete this example and say the handling mechanism for exceptions in Java.

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.