Try-catch-finally
Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals
1, Master try-catch-finally
2, Master try-catch-finally respectively with System.exit (1) and return with the use of precautions
First, finally Execution Process
12345678910111213141516171819202122232425 |
public class Test2 {
public static void main(String[] args) {
Scanner in =
new Scanner(System.in);
System.out.print(
"请输入被除数:"
);
try {
int num1 = in.nextInt();
System.out.print(
"请输入除数:"
);
int num2 = in.nextInt();
System.out.println(String.format(
"%d / %d = %d"
,
num1, num2, num1/ num2));
System.out.println(
"感谢使用本程序!"
);
}
catch
(InputMismatchException ie){
System.out.println(
"输入的类型不匹配"
);
ie.printStackTrace();
}
catch (ArithmeticException e) {
System.err.println(
"除数不能为零"
);
e.printStackTrace();
}
catch (Exception e) {
System.err.println(
"出现错误:被除数和除数必须是整数," +
"除数不能为零。"
);
e.printStackTrace();
}
}
}
|
If you want to make the above code, whether or not an exception occurs, execute the output "Thanks for using this program!" Statement How to Achieve
Add a finally block to the Try-catch statement block and put the statement into the finally block.
12345678910111213141516171819202122232425 |
public class Test4 {
public static void main(String[] args) {
Scanner in =
new Scanner(System.in);
System.out.print(
"请输入被除数:"
);
try {
int num1 = in.nextInt();
System.out.print(
"请输入除数:"
);
int num2 = in.nextInt();
System.out.println(String.format(
"%d / %d = %d"
, num1, num2, num1
/ num2));
}
catch (InputMismatchException ie) {
System.out.println(
"输入的类型不匹配"
);
ie.printStackTrace();
}
catch (ArithmeticException e) {
System.err.println(
"除数不能为零"
);
e.printStackTrace();
}
catch (Exception e) {
System.err.println(
"出现错误:被除数和除数必须是整数," +
"除数不能为零。"
);
e.printStackTrace();
}
finally {
System.out.println(
"感谢您使用本程序!"
);
}
}
}
|
Test
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7F/B3/wKioL1cprcnSfvofAAAHY1BdhKI618.png " data_ue_src= "E:\My knowledge\temp\f61e47f2-b72b-4177-b6b3-78b49febd402.png" >
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7F/B3/wKioL1cprcrhHwlMAABV57h0Ubs382.png " data_ue_src= "E:\My knowledge\temp\f1b3dfa7-3286-41a1-b2af-9b96a31ee99f.png" >
Second, try-catch-finally use precautions(i) the only case where the finally statement is not executed: Exception code block: System.exit (1);
12345678910111213141516171819202122232425 |
public class Test4 {
public static void main(String[] args) {
Scanner in =
new Scanner(System.in);
System.out.print(
"请输入被除数:"
);
try {
int num1 = in.nextInt();
System.out.print(
"请输入除数:"
);
int num2 = in.nextInt();
System.out.println(String.format(
"%d / %d = %d"
, num1, num2, num1
/ num2));
}
catch (InputMismatchException ie) {
System.out.println(
"输入的类型不匹配"
);
ie.printStackTrace();
}
catch (ArithmeticException e) {
System.err.println(
"除数不能为零"
);
e.printStackTrace();
System.exit(
1
);
}
catch (Exception e) {
System.err.println(
"出现错误:被除数和除数必须是整数," +
"除数不能为零。"
);
e.printStackTrace();
}
finally {
System.out.println(
"感谢您使用本程序!"
);
}
}
}
|
(ii) try-catch-finally structure
The try block is mandatory, and the catch and finally blocks are optional, but both must appear
(iii) A return appears in a try or catch
The order in which the exception is executed: Executes the statement before the return in the try block or catch, executes the statement in the finally block, executes the return statement in the try block or catch, and exits.
123456789101112131415161718192021 |
public class Cal {
public String division(String stra, String strb) {
try {
int a = Integer.parseInt(stra);
int b = Integer.parseInt(strb);
int c = a / b;
return String.valueOf(c);
}
catch (NumberFormatException e) {
System.err.println(
"数字格式异常:程序只能接受整数参数"
);
return "数字格式异常"
;
}
catch (ArithmeticException e) {
System.err.println(
"算术异常:除数不能为零"
);
return "算术异常"
;
}
catch (Exception e) {
System.err.println(
"未知异常"
);
return "未知异常"
;
}
finally
{
System.out.println(
"感谢您使用本程序!"跟王老师学异常(四):try-catch-finally
|