PackageCom.swift; Public classEXCEPTION_TESTC { Public Static voidMain (string[] args) {/** Question 5th: There is a class 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. Java Exception Handling mechanism * * If there is a return in a try or catch, execute a try catch finally return,catch to see if there is an exception in the try-throw is an exception thrown Throws is a declaration exception, throw is used in the method body, used to throw an actual exception object, after using throw, either using the try * Catch catch exception, or using throws declaration exception catch can throw an exception again in order to call its methods to catch again and processing * Custom exceptions are used to mark exceptions for business logic and avoid confusion with standard exceptions*/ NewTESTC (). c (); }}classA { Public voidA ()throwsException {Newb (). b (); }}classB { Public voidB () {Try { intA =-1; if(A < 0) { Throw NewMyException ("B ..... You cannot use negative numbers. Throw by Classb.methodb "); } } Catch(MyException e) {e.printstacktrace (); } }}classtestc{ Public voidC () {Try { NewA (). a (); } Catch(Exception e) {e.printstacktrace (); } }}classMyExceptionextendsException { Publicmyexception (String msg) {Super(msg); }}
Exception handling mechanism in Java throw throws custom business logic exception throws continues to throw catch capture and will automatically continue to throw the calling method