throw an exception
There are three forms of throwing exceptions, one is throw, one throws, and the other is automatic throwing of exceptions in the system. The similarities and differences between them below.
The system will automatically throw exceptions. When some logic errors, politic errors or type conversion errors occur in the program statement, the system will automatically throw an exception. like:
public static void main(String[] args) { int a = 5, b =0; System.out.println(5/b); //function(); }
The system will automatically throw an ArithmeticException exception:
Exception in thread "main" java.lang.ArithmeticException: / by zeroat test.ExceptionTest.main(ExceptionTest.java:62)
Like
public static void main(String[] args) { String s = "abc"; System.out.println(Double.parseDouble(s)); //function(); }
The system will automatically throw a NumberFormatException exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "abc" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java :1224)at java.lang.Double.parseDouble(Double.java:510)at test.ExceptionTest.main(ExceptionTest.java:62)
throw
throw is a statement throwing an exception.
Syntax: throw (exception object);
like:
throw e;
It is generally used when a program has a certain logic and the programmer actively throws an exception of a certain type. like:
public static void main(String[] args) { String s = "abc"; if(s.equals("abc")) { throw new NumberFormatException(); } else { System.out.println(s ); } / /function(); }
An exception will be thrown:
Exception in thread "main" java.lang.NumberFormatExceptionat test.ExceptionTest.main(ExceptionTest.java:67)
throws
throws is a declaration that the method may throw an exception. (When declaring a method, it means that the method may throw an exception)
Syntax: [(modifier)] (return value type) (method name) ([parameter list]) [throws(exception class)]{......}
like:
public void function() throws Exception{......}
When a method may throw an exception, it is used to throws to declare an exception that may be thrown, and then hand it over to the method program that calls it. like:
public static void function() throws NumberFormatException{ String s = "abc"; System.out.println(Double.parseDouble(s)); } public static void main(S twist[] args) { try { function(); } catch (NumberFormatException e) { System.err.println("Not data type can't be converted."); //e.printStackTrace(); } }
The processing results are as follows:
Non-data types cannot be converted.
Comparison between throw and throws
1. throws appears in the method function header; throw appears in the function body.
2. throws indicates a possibility of an exception, and these exceptions may not necessarily occur; throw throws an exception, and executing throws a certain exception object must be thrown.
3. Both are ways to deal with exceptions negatively (the negative here does not mean that this method is not good). It is just throwing or may throw an exception, but the function will not handle the exception. The real handling of exceptions is done by the function. The upper-level call processing.
Good programming habits:
1. When writing a program, try{...}catch{...} is usually used to catch and process it for parts that may have exceptions;
2. After catching the exception with try{...}catch{...}, you must process it in catch{...}, even if it is the simplest output statement, or input e on the stack. printStackTrace();
3. If you are catching exceptions in the IO input and output stream, you must add finally{...} to close the input and output stream after try{...}catch{...};
4. If a certain exception is thrown in the function body, it is best to add throws to the function name and then hand it over to the upper function that calls it for processing.
For example:
throws E1, E2, E3 just tells the program that this method may throw these exceptions, and the caller of the method may have to handle these exceptions, and these exceptions E1, E2, E3 may be generated by the function body.
throw makes it clear that this exception is to be thrown in this place.
like:
void doA(int a) throws IOException,{ try{ ...... }catch(Exception1 e){ throw e; }catch(Exception2 e){ System.out.println("Error!"); } if (a!=b) throw new Exception3("Custom Exception");}
There may be 3 exceptions in the code block, (Exception1,Exception2,Exception3).
If an Exception1 exception is generated, it will be captured and thrown again, and the caller of the method will handle it.
If an Exception2 exception is generated, the method handles it itself (i.e. System.out.println("Error!");). Therefore, this method will no longer throw Exception2 exceptions outward, and you don't need to write Exception2 in void doA() throws Exception1, Exception3.
Exception3 exception is an error in a certain segment of the method. The programmer handles it himself. When the segment of the logic is wrong, the exception Exception3 is thrown, and the caller of the method also needs to handle this exception.
The throw statement is used in the method body to indicate that an exception is thrown and is processed by the statements in the method body.
The throws statement is used after the method declaration, indicating that an exception is thrown and handled by the caller of the method.
throws mainly declares that this method will throw this type of exception so that its caller knows to catch this exception.
throw is a specific action to throw an exception outward, so it is to throw an exception instance.
throws means you have that possibility, tendency.
If you throw, that is, you have turned that tendency into reality.
If it is a system exception, you can do nothing, and you can throw an exception to the method, because the system exception can be automatically caught by the system. So whether this exception should be solved inside the method or handed over to the upper-level function to solve the problem is actually effective It's the same. However, I have checked a lot of information. Even if an exception is thrown, it is recommended to write a throws for the method, because this way, other programmers can know what exceptions will appear when completing a large task.
If it is an exception defined by yourself, you must use throws to throw exceptions that may be thrown by this method, otherwise the compilation will report an error.