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