java exception hierarchy Profile

1, abnormal system:

Here Insert Picture Description
Source: https: //blog.csdn.net/zhanaolu4821/article/details/81012382

Trowable has two subclasses: Error and Exception:
Error :
is associated with the virtual machine abnormalities, such as: system crash, dynamic link fails, the virtual machine error (AWTError, IOError), can not capture;
Jvm memory consumed will be reported OutOfMemoryError, after this error occurs, JVM will stop the thread;

Exception
contains RuntimeException and checkException abnormal
RuntimeException is unusual, mainly because programmers do not write the correct code causes abnormal runtime, programmers need to modify the code, rather than be captured,
common RuntimeException have: IndexOfBoundlesException, ArthmaticException, NullException, NumberFormateException, ClassCastException
checkException abnormal non RuntimeException is checkedException, forced programmers to write code in the stage of processing abnormalities, such as FilenotFoundException
You have two options: try catch throws throws in the method declaration

2, try catch finally the execution sequence

Case 1: try {} catch () {} finally {} return;

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}

	private static String trytestFunction() {
        try {
			System.out.println("执行try方法!");
		} catch (Exception e) {
			System.out.println("执行catch方法!");
		}finally {
			System.out.println("执行finally方法!");
		}
		return "44444";
	}
}
执行try方法!
执行finally方法!
44444

Case 2: try {return;} catch () {} finally {} return;


public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
             先执行try块中return 语句(包括return语句中的表达式运算),但不返回;
           执行finally语句中全部代码
            最后执行try中return 返回
    * 
    */
	private static String trytestFunction() {
        try {
			System.out.println("执行try方法!");
			return "try";
		} catch (Exception e) {
			System.out.println("执行catch方法!");
		}finally {
			System.out.println("执行finally方法!");
		}
		return "44444";
	}
}

执行try方法!
执行finally方法!
try

3、 try{ } catch(){return;} finally{} return;

  public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
             有异常:
         执行catch中return语句,但不返回
          执行finally语句中全部代码,
      最后执行catch块中return返回。 finally块后的return语句不再执行
    * 无异常:
    *  正常顺序执行
    */
	private static String trytestFunction() {
        try {
        	throw new Exception("有异常!");
			/* System.out.println("执行try方法!"); */
			
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
		}
	}
}
执行catch方法!
执行finally方法!
catch

4, the case of 4: try {return;} catch () {} finally {return;}

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
            执行try块return语句(包括return语句中的表达式运算),但不返回;
        再执行finally块,
        执行finally块,有return,从这里返回。
    * 
    */
	private static String trytestFunction() {
        try {
			 System.out.println("执行try方法!");
			 return "try";
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
			return "finally";
		}
	}
}
执行try方法!
执行finally方法!
finally

5、try{} catch(){return;}finally{return;}

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
      程序执行catch块中return语句(包括return语句中的表达式运算),但不返回;
再执行finally块,
执行finally块,有return,从这里返回。
    * 
    */
	private static String trytestFunction() {
        try {
			 System.out.println("执行try方法!");
			 throw new Exception("遇到错误!");
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
			return "finally";
		}
	}
}
执行try方法!
执行catch方法!
执行finally方法!
finally

6、try{ return;}catch(){return;} finally{return;}

public class TryTest {

	public static void main(String[] args) {
          String result = trytestFunction();
          System.out.println(result);
	}
   /**
     有异常:

	  执行catch块中return语句(包括return语句中的表达式运算),但不返回;
	   再执行finally块
	   执行finally块,有return,从这里返回。
无异常:
	先执行try块
	再执行finally块
	
	执行finally块,有return,从这里返回。。
    * 
    */
	private static String trytestFunction() {
        try {
			 System.out.println("执行try方法!");
			 return "try";
		} catch (Exception e) {
			System.out.println("执行catch方法!");
			return "catch";
		}finally {
			System.out.println("执行finally方法!");
			return "finally";
		}
	}
}
执行try方法!
执行finally方法!
finally

Summary:
1, after the execution of any try or catch the return statement before returning, if finally it exists, will first execute finally statement;
2, if there is a return statement finally, then the program will return, so finally in return It is sure to be a return;

3, several common Exception

(This section is referenced from https://blog.csdn.net/zhanaolu4821/article/details/81012382)
java.lang.IllegalAccessError: illegal access error. When an application attempts to access, modify a class field (Field) or call its methods, but it is visibility statement violates domain or methods, Thrown.

java.lang.InstantiationError: instantiation error. The exception is thrown when an application attempts an abstract class or interface configured Java new operator time.

java.lang.OutOfMemoryError: Out of memory error. This error is thrown when the available memory is not sufficient to make the Java virtual machine is assigned to an object.
java.lang.StackOverflowError: stack overflow error. This error is thrown when an application calls the recursive hierarchy too deep and causes a stack overflow or into an infinite loop.

java.lang.ClassCastException: class cast exceptions. Assume that type A and B (A B is not the parent class or subclass), examples of A is O, then forced out of the anomaly when configured as an O Example B class. The exception is often referred to as cast exception.
java.lang.ClassNotFoundException: class not found exception. When an application attempts according to a string of the class name of the class, and after traversing CLASSPAH not find the corresponding name of the class file, Thrown.

java.lang.ArithmeticException: arithmetic abnormal conditions. For example: integer divide by zero and so on.
java.lang.ArrayIndexOutOfBoundsException: Array index out of bounds exception. When thrown array index value is negative is equal to or greater than the array size.

java.lang.IndexOutOfBoundsException: index out of bounds exception. When access to a sequence of index values less than 0 or greater than or equal size sequence, the thrown exception.
java.lang.InstantiationException: Examples exception. When creating an instance of a class trying to newInstance () method, and the class is an abstract class or interface, Thrown.

java.lang.NoSuchFieldException: property there is no abnormality. Thrown when there is no access to the properties of a class.
java.lang.NoSuchMethodException: for exception does not exist. Thrown when an access method does not exist in a class.
java.lang.NullPointerException: null pointer exception. When an application attempts to use a null object at the required place, Thrown. For example: Example of a null object method invocation, property access null object, calculating the length of a null object, null, etc. using throw statement.
java.lang.NumberFormatException: digital format exception. When trying to convert a String number specified type, and the string does not satisfy the requirements of the format of the digital type, the thrown exception.

java.lang.StringIndexOutOfBoundsException: String index out of bounds exception. When accessing a character string using the index value, the index value is less than 0 or greater than or equal size sequence, the thrown exception.

Published 15 original articles · won praise 7 · views 2855

Guess you like

Origin blog.csdn.net/xiaocaodeshengri/article/details/100761821