exception handling Mechanisms in Java
Example
static void main(String[] args) {// TODO Auto-generated method stubString s="hello";int i=Integer.parseInt(s);}}
Run Exception results
In the above code, integer.parseint means to convert the string type to an integer type, while the integer is an int wrapper class, the program will error, because the computer has no way to convert hello to an integer, then you can use the following ways to solve the problem
public Class test {/** * Try block indicates something that may be wrong * The CATCH block is the part that executes after the error * */public static void main (String[] args) {//TODO auto-generated method stubstring s=try {int i=integer.parseint (s); catch (NumberFormatException e) {/** * * * program does not go wrong when it is not executed in the CATCH block //TODO auto-generated catch Blocke.printstacktrace ();}}
You can also do this in a catch block: System.out.println (e.getmessage)----Print out Error statement information;; E.printstacktrace () and print stack information; The events in the above example can also be handled or not handled
Errors that must be handled
public static void main(String[] args){ Class.forName("java.test");}
The error thrown is: Unhandle Exception type classnotfoundexception--> class did not find an exception error, this error system requirements must be handled, processing the same way as try{}catch{} to handle
Exception handling mechanism: An error that occurs when a program is running, which causes the program to not function correctly is called an exception
- Error: General JVM run errors, no processing, no way to handle
- Exception is divided into runtimeexception and non-runtimeexception, run exception can choose to handle or not to handle, non-running exception can not be too much
Summary of Exception types
RuntimeException:
-
-
- nullfointerexcption--> NULL pointer exception
- Indexoutofboundexception--> array subscript out of bounds exception
- Numberformatexception--> Data Format exception
- Classcastexception--> Type Conversion exception
- Illegalargumentexception--> Illegal parameter exception
- Arithmeticexception--> Arithmetic exception
Non-runtimeexception:
-
-
- Classnotfoundexception--> Class Load exception
exception handling Mechanisms in Java