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
- Package Com.itheima;
- Import java.io.IOException;
- /**
- * Question 6th: There is a class ClassA, there is a class of CLASSB, in ClassB there is a method B,
- * This method throws an exception, there is a method a in the ClassA class, call B in this method, and throw an exception.
- * In the client there is a class of TESTC, there is a method for C, please catch the exception information in this method.
- * To complete This example, please describe the handling mechanism for exceptions in Java.
- *
- Exceptions in *java are divided into "Runtime exceptions" and "compile-time exceptions"
- * 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
- * Compile-time exception: is the program in the process of compiling the exception generated, this we have to deal with
- * In Java programs, there are two kinds of exception handling mechanism, one is "throw exception", one is "catch exception", one is "default out"
- * 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
- * Throw exception: Use the keyword throws throw
- * Throws is used to throw an exception when declaring a method, only an exception
- * Throw is used to throw an actual exception, throw a specific exception object, can be more than one exception.
- * Catch Exception: try....catch...finally
- * try{code block}
- * The code block that is contained in the middle is the code that may appear to be abnormal.
- * catch{code block}
- * This code block is the code to handle the exception
- * finally{code block}
- * This is a block of code that needs to be executed after exception handling. This block of code is always executed to release resources.
- * Note: Try cannot be used alone, it must be used with one of the catch or finally two
- */
- Public class Test6 {
- Public static void Main (string[] args) throws Exception {
- Create TC Object
- TESTC TC = new TESTC ();
- Use TC to invoke the C method and run this class
- TC.C ();
- A = new A ();
- A.A ();
- }
- }
- Class A
- Class a{
- 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
- public Void A () throws exception{
- b b = new B ();
- B.B ();
- }
- }
- Class B
- Class B {
- Create a B method and throw an exception
- public void B () throws exception{
- System.out.println ("I am a B method in class B, I throw an exception!") ");
- }
- }
- Client class
- Class testc{
- public void C () {
- Create a object of a
- A = new A ();
- 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
- try {
- A.A ();
- } catch (Exception e) {
- //TODO auto-generated catch block
- System.out.println ("I am the catch exception method," +e.tostring ());
- }
- }
- }
Complete this example and say the handling mechanism for exceptions in Java.