java java ---- basis of common exceptions and code samples

Outline

java There are two types of errors, one Exception, one Error, both in the java.lang package, general programs try ... catch is caught Exception types of exceptions, and Error type of error should not capture, because error type of error is very serious, even if it is difficult to capture this error automatically processed in the program. For example java.lang.OutOfMemoryError, this error, the program even if it is difficult to capture the process, because it can produce the abnormal procedures written description of a problem, or heap memory is unreasonable, often requires manual processing.

 

Exception class structure in FIG.

 

 As it can be seen from the figure, Error Exception and inherit the class Throwable. Add here that, in an abnormal Exception is divided into two, one is automatically detected by the compiler, and the other is the error logic program, the compiler does not automatically detect the figure belongs to the compiler will not RuntimeException detection of abnormalities, and the remaining two are part of the compiler will automatically detect (of course, there are many exceptions under the exception, not all listed here).

This article describes a total of five common exception type, as follows:

java.lang.OutOfMemoryError
java.lang.StackOverflowError
java.lang.IndexOutOfBoundsException
java.lang.NullPointerException
java.lang.OutOfMemoryError

The sample code

package com.gxl.demo.exception;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.UUID;


public class ExceptionTest {

    / ** null pointer exception -java.lang.NullPointerException * / 
    public  static  void nullPointerExceptionTest () {
        String a = null;
        System.out.println(a.length());
    }

    / ** memory limits exception -java.lang.OutOfMemoryError * / 
    // During the execution of VM options provided -Xms20M -Xmx20M -Xmn10M performing 
    public  static  void outOfMemoryErrorTest () {
        List list = new ArrayList();
        for (int i = 0; i < 20; i++) {
            byte[] bytes = new byte[1024*1024];
            list.add(bytes);
        }
    }
    / ** stack overflow exception -java.lang.StackOverflowError * / 
    public  static  void stackOverflowErrorTest () {
         int A =. 1 ;
        stackOverflowErrorTest();
    }
    / ** concurrent modifications abnormal -java.util.ConcurrentModifyException * / 
    public  static  void concurrentModifyExceptionTest () {
        List list = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            new Thread(() -> {
                try {
                    list.add(UUID.randomUUID().toString().substring(20));
                    System.out.println(list);
                } catch (ConcurrentModificationException e) {
                    System.out.println("====> java.util.ConcurrentModifyException");
                    System.out.println(e.fillInStackTrace());
                }
            }, String.valueOf(i)).start();
        }
    }


    / * Index exceeds limit -java.lang.IndexOutOfBoundsException * / 
    public  static  void indexOutOfBoundsExceptionTest () {
        List list = new ArrayList();
        list.add("2");
        list.get(1);
    }


    public static void main(String[] args) {
        try {
            ExceptionTest.nullPointerExceptionTest();
        } catch (Exception e) {
            System.out.println("====> java.lang.NullPointerException");
            System.out.println(e.fillInStackTrace());
        }

        // Here's error really should not be captured here to demonstrate, captured 
        the try {
            ExceptionTest.outOfMemoryErrorTest();
        } catch (OutOfMemoryError  e) {
            System.out.println("====> java.lang.OutOfMemoryError");
            System.out.println(e.fillInStackTrace());
        }
        // Here's error really should not be captured here to demonstrate, captured 
        the try {
            System.out.println("====> java.lang.StackOverflowError");
            stackOverflowErrorTest();
        } catch (StackOverflowError e) {
            System.out.println(e.fillInStackTrace());
        }


        concurrentModifyExceptionTest ();

        try {
            indexOutOfBoundsExceptionTest ();
        } catch (Exception e) {
            System.out.println("====> java.lang.IndexOutOfBoundsException");
            System.out.println(e.fillInStackTrace());
        }

    }

}

to sum up

Exception common types of exceptions are generally easier to handle, more trouble is Error type of error, because generally produce Error type of error is often generated jvm virtual machine, often requires jvm GC mechanism, the heap, stack comparison Learn to resolve, then write about jvm related blog.

 

Sao words


All unpleasant life can be attributed to conflicts, their inner conflicts, such as confusion. Own and others' conflicts, such as the contempt of others, can not find the target. Conflicts themselves and society, such that no student in good times, no car, no house. That how to solve all these problems ,,, only one way, that is to say the year Xiaoping grandfather - development, development is the last word.

Guess you like

Origin www.cnblogs.com/gunduzi/p/12031974.html