Throws keywords
When defining a method, you can use the throws keyword declaration. The method declared with the throws keyword means that this method does not handle the exception and is passed to the method call for processing .
Throws keyword format:
Public return value type method name (parameter list,,) throws exception class {};
Suppose a division is defined, an exception may occur for a division operation, and may not be. So for this method it is best to use the throws keyword declaration, and once an exception occurs,
It should be handled at the call.
classmath{ Public intDivintIintJthrows Exception{//defines the division operation, and if there is an exception, it is handed to the manipulated inttemp = i/j;//calculated, but there may be exceptions here returntemp; }}; Public classthrowsdemo01{ Public Static voidMain (String args[]) {Math m=NewMath ();//instantiate the Math class object Try { System.out.println ("Division Operation:" +M.Div (10,2)) ; }Catch (Exception e) { e.printstacktrace (); //Print exception } }};
Because the DIV uses the throws keyword declaration, the method must be handled with exception when calling this method. Through Try...catch;
If the throws keyword is also used in the declaration of the main method, does it mean that the main method can also not handle the exception.
classmath{ Public intDivintIintJthrows Exception{//defines the division operation, and if there is an exception, it is handed to the manipulated inttemp = i/j;//calculated, but there may be exceptions here returntemp; }}; Public classthrowsdemo02{//All exceptions in the main method can be handled without using Try...catch Public Static voidMain (String args[])throwsexception{Math m=NewMath ();//instantiate the Math class objectSYSTEM.OUT.PRINTLN ("Division operation:" + M.Div (10,0)) ; }};
Operation Result:
Exception in thread "main" java.lang.ArithmeticException:/By zero
At Methoud. Math.div (Thisdemo06.java:4)
At Methoud. Thisdemo06.main (Thisdemo06.java:12)
In this program, the main method does not handle any exceptions, but to the largest JVM in Java, so if the main method uses the throws keyword, it means that all exceptions
Given to the JVM for processing. The default processing is also done by the JVM.
throw keyword
Throw off the word action is to throw an exception, throw the time is thrown is an exception class instantiation object,
In exception handling, the Try statement is to catch an exception object, and the exception object can be thrown by itself.
Package Methoud; Public class thisdemo06{ publicstaticvoid main (String args[]) { try { thrownewException("Throw yourself in the game.") ") ; //Throws an exception to the instantiated object } Catch (Exceptione) { System.out.println (e); }};
example, throw and application of throws
In general development, the use of try,,catch,,finally,throw,throws is the most common.
For example, you now want to use a divided method, but you must print the "Operation start" message before the operation, and you must print "End of exception" after the end.
If there is an exception, you need to pass the exception to the exception call processing. In the face of this requirement, all of the above keywords must be used.
classmath{ Public intDivintIintJthrowsexception{//defines the division operation, and if there is an exception, it is handed to the manipulatedSYSTEM.OUT.PRINTLN ("* * * * * Calculation starts * * * * *) ;int Temp= i/j;//calculated, but there may be exceptions hereSYSTEM.OUT.PRINTLN ("* * * * calculation is OVER * * * * *) ; returntemp; }}; Public classthrowdemo02{ Public Static voidMain (String args[]) {Math m=NewMath (); Try{System.out.println ("Division operation:" + M.Div (10,0)) ; }Catch(Exception e) {System.out.println ("Exception generated:" +e); } }};
Operation Result:
Calculation Starts * * * *
Exception generation: Java.lang.ArithmeticException:/by zero
The above calculation is not finished, because no exception occurred, directly interrupt the program operation. So do the following.
PackageMethoud;classmath{ Public intDivintIintJthrowsexception{//defines the division operation, and if there is an exception, it is handed to the manipulatedSYSTEM.OUT.PRINTLN ("* * * * * Calculation starts * * * * *) ; inttemp = 0;//Defining local Variables Try{Temp= i/j;//calculated, but there may be exceptions here}Catch(Exception e) { }SYSTEM.OUT.PRINTLN ("* * * * calculation is OVER * * * * *) ; returntemp; }}; Public classthisdemo06{ Public Static voidMain (String args[]) {Math m=NewMath (); Try{System.out.println ("Division operation:" + M.Div (10,0)) ; }Catch(Exception e) {System.out.println ("Exception generated:" +e); } }};
Operation Result:
Calculation Starts * * * *
End of calculation * * *
Exception generation: Java.lang.ArithmeticException:/by zero
Although it seems to be successful here, but the exception here is not thrown out, because in the method has been automatically processed, not thrown away.
So the exception object is thrown, handled at the method call, and the Throw keyword is used.
PackageMethoud;classmath{ Public intDivintIintJthrowsexception{//defines the division operation, and if there is an exception, it is handed to the manipulatedSYSTEM.OUT.PRINTLN ("* * * * * Calculation starts * * * * *) ; inttemp = 0; //define local variable try {Temp= i/j;//calculated, but there may be exceptions here}Catch(Exception e) {Throw E; Throws an exception. }finally{//no matter if there is an exception, you have to perform a unified exportSYSTEM.OUT.PRINTLN ("* * * * calculation is OVER * * * * *) ; } returntemp; }}; Public classthisdemo06{ Public Static voidMain (String args[]) {Math m=NewMath (); Try{System.out.println ("Division operation:" + M.Div (10,0)) ; }Catch(Exception e) {System.out.println ("Exception generated:" +e); } }};
The difference between exception and runtimeexception
is a recurring problem in the interview.
Observe the following code:
Package Methoud; Public class thisdemo06{ publicstaticvoid main (String args[]) { = "123"; // defines a string, all of which consist of numbers int Integer.parseint // to change a string to an int type SYSTEM.OUT.PRINTLN (temp * temp); // Calculate the exponentiation }};
parseint () defines the format:
Public Static int throws NumberFormatException
This method obviously uses the throws keyword throws the exception, why does not have to deal with, also can compile passes?
In the Java exception handling mechanism,
1) If the type of exception is thrown, you must make a try: catch to handle.
2) If the type of runtimeexception is thrown, you can not use try: Catch processing, which is handled by the JVM once an exception occurs.
In order to ensure the health of the program, it is possible to use the try when there is an exception. Catch processing.
Custom exception classes.
You can customize the exception class only if you inherit the exception class . Because Java provides a standard exception class (including some exception information), if you want to
You can customize the exception class with the exception information.
classMyExceptionextendsexception{//Custom exception classes, inheriting the exception class Publicmyexception (String msg) {Super(msg);//call a constructor that has a parameter in the exception class, passing the error message }}; Public classdefaultexception{ Public Static voidMain (String args[]) {Try{ Throw NewMyException ("Custom exception. ") ;//Throw Exception}Catch(Exception e) {System.out.println (e); //Print error message } }}
Operation Result:
Methoud. MyException: Custom Exception.
Summarize
The throw and throws keyword are used in conjunction with the problem.
1) Throw: throws an exception.
2) throws: Used at the method declaration to indicate that this method does not handle the exception, and that the exception is handled at the call of this method.
Exception must be handled, and runtimeexception exceptions can be unhandled. But in order to ensure the normal operation of the program, the best deal.
If you customize the exception, you can inherit the exception directly.
Throw and throws keywords