On JAVA exception mechanism

1. If a transaction method set spring and spring affairs into effect, an exception is encountered an exception is thrown, the operation is rolled back.

2. In the code loop, an exception is thrown after the code stops executing, resulting in a complete code can not run.
The solution is simple, catch the exception and to handle it can be simple.

public void getTest() {
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
list.add("e"); //转数字抛出异常
list.add("4");

for (String str : list) {
try {
int num = Integer.parseInt(str); //异常
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
//continue;
}

System.out.println("***");
}
}

 

Guess you like

Origin www.cnblogs.com/gulin168/p/11068902.html