[Notes] from scratch to learn Java exception handling

We can focus on the author's account, focus on learning Java from scratch notes anthology. We can also learn from a catalog visit the author's blog garden blog. The film documents the learning and the sharing of information on employment-based programmers dark horse class video, and record notes and their own views. Welcome to learn and discuss.

[Notes] from scratch to learn Java directory

What is abnormal?

Abnormality is not normal, error occur when compiling our code or runtime. When we write code, often some small problems, then in order to help us deal with these problems, java exception provides a mechanism for us. Exception contains the wrong type, location and cause of
the abnormal architecture:
Throwable (most entry level)
Error: Serious problems can arise not handled
Exception: can deal with problems

Unusual approach

1.jvm way to handle exceptions:
If an exception occurs we do not deal with, jvm will help us to deal with, he would unusual type, because there do not appear in the command line, and also terminated the program, exception code will not be back carried out.

2. Capture process
try ... catch statement
format:

 try {
  有可能出现问题的代码;
} catch(ArithmeticException ae) {
  处理异常;
    }

The order of execution try ... catch:
First, the try statement, if unusual, abnormal following code is not executed directly jump into the catch statement, statement after the catch, the whole try ... catch end, if not unusual, after the statement is executed try, try ... catch directly to end, not executing catch statement

3. thrown out
when we do not want to handle the exception, or the inability to deal with, we can choose to throw an exception, who calls the method who handle exceptions, use the keyword throws in the declaration of a method throws an exception

For example:

import java.io.FileWriter;
import java.io.IOException;


public class ExceptionDemo2 {
	public static void main(String[] args) throws Exception {
		//method();
		
		function();
		
	}
	
	public static void function() throws Exception {
		FileWriter fw = new FileWriter("a.txt");
	}

	private static void method() {
		try {
			System.out.println(1);
			//System.out.println(2 / 0);
			System.out.println(2);
		} catch(ArithmeticException ae) {
			System.out.println("除数不能为0");
		}
		
		System.out.println(3);
	}

}

Handling multiple exceptions

How to deal with multiple exceptions:
1. You can use a plurality of try .. .catch statement
2. Using a plurality of try and catch

Sequence among the plurality of catch:
there may be between the plurality of sub parent catch
no order between the same level
if the child parent, the parent class must be placed behind the exception

Throwable类

public class ExceptionDemo3 {
	public static void main(String[] args) {
		try {
			System.out.println(2 / 0);
		} catch (ArithmeticException e) {
			// String getMessage() : 原因
			// System.out.println(e.getMessage());

			// String toString() 类型和原因
			// System.out.println(e.toString());

			// void printStackTrace():类型原因和位置,和jvm处理不同,不会终止程序运行
			e.printStackTrace();
		}

		// System.out.println("hello");
	}

}

finally

finally: try ... catch combination use, such as finishing work for the release of resources, regardless of the try ... catch statement is executed, finally given code executes, used to treat some of the finishing touches, such as the release of resources

try {
    有可能出现问题的代码;
} catch(异常对象) {
    处理异常;
} finally {
    释放资源;
    清理垃圾;
}

Unusual classification

Run an abnormal: RuntimeException is a subclass of runtime exceptions, are free to choose treatment at compile time or do not deal with
compile-time exception: Exception is a subclass of non-RuntimeExcpetion subclass must be handled at compile time

Guess you like

Origin www.cnblogs.com/zllk/p/12656772.html