Exceptions in Java
- Exception exception we can deal with, can be remedied, encountered an exception will not be executed?
Package cn.seven.shengsiyuan.exception;/* * time:2015 April 05 12:24:58, Ching Ming Festival * location:501 * */public class ExceptionDemo1 {public static void main (string[] args) {//TODO auto-generated method Stubint A = 3;int b = 0;int C = A/ b;//This sentence execution will occur an exception, the following statement will not execute SYSTEM.OUT.PRINTLN ("------"); System.out.println (c);}}
/*
Exception in thread "main" java.lang.ArithmeticException:/By zero
At Cn.seven.shengsiyuan.exception.ExceptionDemo1.main (exceptiondemo1.java:17)
*/
- Exceptions in the exception inheritance system-java inherit from the exception class exception into two main categories:
Checked Exception (non-runtime exception)
Unchecked Exception (runtime exception runtime Exception)
Java.langclass throwable java.lang.Object java.lang.Throwable all implemented Interfaces: Serializable Direct known subclasses:
Java.langclass Exception java.lang.Object java.lang.Throwable java.lang.Exception
-
- Run-time Exception runtimeexception
Java.lang.Object java.lang.throwable- java.lang.Exception java.lang.RuntimeException Javax.management.JMRuntimeException
- Non-runtime exception: inherits the exception class, but does not inherit exceptions from the RuntimeException class
- RuntimeException exception handling-fixed processes and patterns
Try{}catch (Exception e) {}
Package cn.seven.shengsiyuan.exception;/* * time:2015 April 05 12:24:58, Ching Ming Festival * location:501 * */public class ExceptionDemo1 {public static void main (string[] args) {//TODO auto-generated method Stubint c = 0;try{int A = 3;int B = 2; C= A/b; SYSTEM.OUT.PRINTLN ("---hello---");} catch (ArithmeticException e) {//assigns the exception object generated by 19 rows to Ee.printstacktrace ();//printstacktrace printing exception information from the Throwable class method} Finally{system.out.println ("finally");} SYSTEM.OUT.PRINTLN ("------"); System.out.println (c);}} /* * 19 rows with an exception, resulting in an object for the exception class ArithmeticException * When an exception occurs, enter the catch block, take over by the catch, and then execute the contents of the Catch block * execution is complete, then execute the SYSO statement * * * The focus now is on the 21-row-catch statement process */
- Complete exception-handling structure
try{//the code block of a statement that may have an exception}catch (Exception object) {//For processing after an exception occurs}finally{//the statement that will eventually be executed regardless of whether an exception occurred, preventing some statements from being executed because an exception occurred}
Package cn.seven.shengsiyuan.exception;/* * time:2015 April 05 12:24:58, Ching Ming Festival * location:501 * */public class ExceptionDemo1 {public static void main (string[] args) {//TODO auto-generated method Stubint c = 0;try{int A = 3;int B = 0; C= A/b; SYSTEM.OUT.PRINTLN ("---hello---");} catch (ArithmeticException e) {e.printstacktrace ();//printstacktrace printing exception information, methods from the Throwable class}finally{ System.out.println ("finally");//must be executed regardless of whether an exception occurred}//system.out.println (c); The question is why is c=0 still output here? Because of c=a/b; statement exception, C no assignment statement}}
- If there are multiple exceptions in the program, only specific exceptions will be handled by the particular catch.
Think in Java Notes _chapter12_1_exception Basics _ Inheritance and runtimeexception processing 1