Java self - Exception Handling Exception Category

Java exception classification

Abnormal Category: abnormalities to be investigated, exceptions, and three kinds of run-time errors
in which run-time exceptions and errors known as non unchecked exception

Step 1: be checked exception

Can be checked exception: CheckedException
be checked exception that is necessary exception handling , or try catch live, or throw out, who to call, who deal with, such as FileNotFoundException
if not addressed, the compiler will not let you through

package exception;
  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
  
public class TestException {
  
    public static void main(String[] args) {
          
        File f= new File("d:/LOL.exe");
          
        try{
            System.out.println("试图打开 d:/LOL.exe");
            new FileInputStream(f);
            System.out.println("成功打开");
        }
        catch(FileNotFoundException e){
            System.out.println("d:/LOL.exe不存在");
            e.printStackTrace();
        }
          
    }
}

Step 2: runtime exception

RuntimeException refers to abnormal runtime: than having to try catch exception
common runtime exception:
the divisor can not be zero anomalies: ArithmeticException
subscript bounds exception: ArrayIndexOutOfBoundsException
null pointer exception: NullPointerException

When writing code, you can still use try catch throws for processing, and can be found abnormal difference is that, even if not to try catch, there will be no compilation errors
one cause of the exception reason when Java is designed to run, is because the index out of bounds, null pointer exception these run too common , if need be captured, the readability of the code will become very bad.

package exception;
  
public class TestException {
  
    public static void main(String[] args) {
         
        //任何除数不能为0:ArithmeticException
        int k = 5/0;
         
        //下标越界异常:ArrayIndexOutOfBoundsException
        int j[] = new int[5];
        j[10] = 10;
         
        //空指针异常:NullPointerException
        String str = null;
        str.length();
   }
}

Step 3: Error

Error Error, refer to the system-level exception , usually run out of memory
at default settings , general java program starts when the maximum memory 16m can be used
as examples to stop StringBuffer append a character, and soon put the memory the use of light. Throw OutOfMemoryError
an exception, as it is not mandatory to capture error when running

package exception;
  
public class TestException {
  
    public static void main(String[] args) {
     
        StringBuffer sb =new StringBuffer();
         
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            sb.append('a');
        }
         
    }
 
}

Step 4: three categories

Overall abnormal divided into three categories:

  1. error
  2. Runtime exception
  3. Anomalies to be investigated
    Three categories

Exercise : the Java malfunction classification

RuntimeException abnormal operation, can be captured?

Error Error, could be captured?

Face questions often problems: abnormal difference with non-runtime exception runtime

The answer :

All OK.

package exception;
 
public class TestException {
 
    public static void main(String[] args) {
 
        String str = null;
 
        try {
            str.toString();
        } catch (NullPointerException e) {
            System.out.println("捕捉到运行时异常: NullPointerException ");
        }
 
        StringBuffer sb = new StringBuffer("1234567890");
        try {
            for (int i = 0; i < 100; i++) {
                sb.append(sb.toString());
            }
        } catch (OutOfMemoryError e) {
            System.out.println("捕捉到内存用光错误:  OutOfMemoryError");
        }
 
    }
}

Exceptions Exceptions distinction runtime and non-run time:
exceptions are not checked abnormal operation, does not require explicit capture
abnormality is to be investigated when the non-operation exception, and must be explicitly captured, or throws

Guess you like

Origin www.cnblogs.com/jeddzd/p/11670673.html