Study notes: java errors and exceptions

error

Hardware or operating system errors

abnormal

Action taken outside of normal control flow due to an error in the program.
Catch exceptions
(1) guarded region: a piece of code that may generate exceptions, and follow the code to handle these exceptions;
(2) Java uses try{} block to place exceptions that may throw exceptions, and use catch The block captures all exceptions thrown by the try block, and uses the finally block to clear them;

try{
    
       检查语句序列;}
catch(Exception ex){
    
        异常发生的处理语句 }
finally{
    
        一定要执行的语句}

Common exception

RuntimeException
wrong type conversion array subscript out of bounds null pointer access
IOExeption
read data from a non-existent file beyond the end of the file to continue reading EOFException connect to a non-existent URL

For example

public class Test01{
    
    
    public static void main(String[] args){
    
    
        String friends[] = {
    
    "lisa","bily","kessy"};
        try{
    
    
            for(int i=0;i<5;i++) {
    
    
                System.out.println(friends[i]);
            }
        }catch(ArrayIndexOutOfBoundsException e){
    
    
            System.out.println("index err");
        }
        System.out.println("\nthis is the end");
    }
}
程序Test01运行结果:lisa    bily    kessy
        index err this is the end   

public class DivideZero1{
    
    
    int x;
    public static void main(String[] args) {
    
    
        int y;
        DivideZero1 c=new DivideZero1();
        try{
    
    
            y=3/c.x;
        }
        catch(ArithmeticException e){
    
    
            System.out.println("divide by zero error!");
        }
        System.out.println("program ends ok!");
    }
}
程序DivideZero1运行结果:
        divide by zero error!
        program ends ok!


Declare an exception

Declaring that an exception is thrown is the second way to handle exceptions in Java.
If a method (when the statement is executed) may generate some kind of exception, but it is not sure how to handle such an exception, then this method should explicitly declare that it throws Exceptions indicate that the method will not handle these exceptions, but the caller of the method is responsible for handling them.
The throws clause can be used in the method declaration to declare the list of thrown exceptions. The exception type after throws can be the exception type generated in the method or its parent class.
Declare an exception example:

public void readFile(String file)  throws FileNotFoundException {
    
    
        ……
        // 读文件的操作可能产生FileNotFoundException类型的异常
        FileInputStream fis = new FileInputStream(file);
        ..……
        }

Manually throw an exception

Java exception class objects are automatically generated and thrown by the system when an exception occurs during program execution, or they can be created and thrown manually as needed.
First, an exception class object is generated, and then the throwing operation is implemented through the throw statement (submitted to Java to run surroundings).
IOException e = new IOException();
throw e;
The exception that can be thrown must be an instance of Throwable or its subclasses. The following statement will produce a syntax error at compile time:

public class Test01{
    
    
    public void regist(int num) throws MyException {
    
    
        if (num < 0)
            throw new MyException(“人数为负值,不合理”,3);
        else
            System.out.println("登记人数" + num );
    }
    public void manager() {
    
    
        try {
    
    
            regist(100);
        } catch (MyException e) {
    
    
            System.out.print("登记失败,出错种类"+e.getId());
        }
        System.out.print("本次登记操作结束");
    }
    public static void main(String args[]){
    
    
        Test8_6 t = new Test8_6();
        t.manager();

Attached
springBuffer use case

package tyichang;

public class E18 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("------------------------");
        add();
        System.out.println("------------------------");
        update();
        System.out.println("------------------------");
        delete();
    }

    public static void add() {
    
    
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("ABC");
        System.out.println(stringBuffer);
        stringBuffer.append("HER");
        System.out.println(stringBuffer);
        stringBuffer.insert(1, "de");
        System.out.println(stringBuffer);

    }

    public static void update() {
    
    
        StringBuffer stringBuffer = new StringBuffer("AAAAAAA");
        stringBuffer.setCharAt(2, 'C');
        System.out.println(stringBuffer);
        stringBuffer.replace(3, 5, "de");
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.reverse());
    }

    public static void delete(){
    
    
        StringBuffer stringBuffer = new StringBuffer("AAVAAAAA");
        stringBuffer.delete(3,5);
        System.out.println(stringBuffer);
        stringBuffer.deleteCharAt(2);
        System.out.println(stringBuffer);
        stringBuffer.delete(0,stringBuffer.length());
        System.out.println(stringBuffer);
    }
}

Guess you like

Origin blog.csdn.net/qq_44909275/article/details/105481330