Java Exception Handling Mechanism throw throws custom business logic exception throws continue to throw catch capture will automatically continue to throw the call method, throwthrows

Source: Internet
Author: User
Tags try catch

Java Exception Handling Mechanism throw throws custom business logic exception throws continue to throw catch capture will automatically continue to throw the call method, throwthrows

Package com. swift; public class Exception_TestC {public static void main (String [] args) {/** 5th question: one class is ClassA and one class is ClassB, there is a method B in ClassB. This method throws an exception. There is a * method a in the ClassA class. Please call B in this method and then throw an exception. There is a class TestC on the client, and there is a method of c. Capture exception information in this method. Java Exception Handling Mechanism ** if return exists in a try or catch, execute try catch finally return. If catch is executed, check whether an exception exists in a try. * throw throws and declares an exception, throw is used in the method body to throw an actual exception object. After throw is used, either try * catch is used to catch an exception or throws is used to declare an exception. In the catch, throw can throw an exception again, so that you can call the method to catch and handle the * custom exception used to mark the exception of the business logic to avoid confusion with the standard exception */new TestC (). c () ;}} class A {public void a () throws Exception {new B (). B () ;}} class B {public void B () {try {int a =-1; if (a <0) {throw new MyException ("B .... negative numbers cannot be used .. throw by ClassB. methodB ") ;}} catch (MyException e) {e. printStackTrace () ;}} class TestC {public void c () {try {new (). a ();} catch (Exception e) {e. printStackTrace () ;}} class MyException extends Exception {public MyException (String msg) {super (msg );}}

 

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.