Java Basics-Exception-throws exception-throw declaration

Source: Internet
Author: User

Call someone else's program method, and someone else's method declares that there may be an abnormal invocation of the place must have two ways of processing (or ecplise will prompt syntax error): 1, also do the exception declaration, the exception is not handled, received the exception I also thrown out----if there is no other place to receive this exception, Then the exception is received by the JVM, and then call the JVM default exception handling mechanism, abort program 2, the exception is Try-catch preprocessing 1: Make exception declaration
 Public class exceptiontest {                                         //declaration: Exception not handled, received exception I also throw outward
     Public Static voidMain (string[] args)throwsexception{Divdemo DD=NewDivdemo (); intResultd = Dd.div (10, 0); System.out.println ("Resultd ' value is:" +Resultd); System.out.println ("Over!"); }}classdivdemo{//functionally through the throws keyword--//--declaration: Calling this method may cause problems, and may throw out exceptions     Public intDivintAintbthrowsException {returnA/b; }}

: Console:

"Over!" not be printed

2:try--catch processing

 Public classExceptiontest { Public Static voidMain (string[] args) {Divdemo dd=NewDivdemo (); intResultd; Try{Resultd= Dd.div (10, 0); System.out.println ("Resultd ' value is:" +Resultd); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Over!"); }}classdivdemo{//functionally through the throws keyword--//--Declaring a call to this method may cause problems     Public intDivintAintbthrowsException {returnA/b; }}

: Console:

"over!" Printing, program completion

Java Basics-Exception-throws exception-throw declaration

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.