Java throws Exception, try, catch

Source: Internet
Author: User
Tags throw exception java throws

Throws exception is the method followed by means of throwing an exception to the superior

The exception in try{} is caught by an outside catch

Throw exception is throw new Exception ("exception");

Use the following

12345678910 publicstaticvoidmain(String args[]){    try{        A();    }catch(Exception e){        System.out.println(e.getmessage());    }}publicstaticvoidA() throwsException{    throw newException("A方法抛出了一个异常");}

(1) If you do not want to write specific code to catch the exception, you can use the form of throws Exception,
Throws the exception again, to the JVM (the Java virtual machine) to Capture. This is a more convenient way.
(2) If you want to write code that handles exceptions, you can use the form of Try{}catch () {} to capture,
Once the program has an exception, it will be executed according to the code written by your catch{} Block.

Throws used on methods to declare that the method has an exception that can be thrown
Try{}: catch exception
Catch: Handling Exceptions

Java throws Exception, try, catch

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.