异常 基础点练习
-
运行异常与一般异常有何异同 ?
运行异常在编译期不检查,出现问题后需要修改代码
非运行异常编译器就必须处理,否则不能通过编译
-
JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?
throws是获取异常
throw是抛出异常
try是将会发生异常的语句括起来,从而进行异常的处理,也可以在try块中抛出新的异常
catch是如果有异常就会执行他里面的语句
finally不论是否有异常都会进行执行的语句
-
try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
会执行,在方法返回调用者前执行。Java允许在finally中改变返回值的做法是不好的,因为如果存在finally代码块,try中的return语句不会立马返回调用者,而是记录下返回值待finally代码块执行完毕之后再向调用者返回其值
-
写出程序结果 : class Demo { public static void main(String[] args) { try { showExce(); System.out.println("A"); } catch (Exception e) { System.out.println("B"); } finally { System.out.println("C"); } System.out.println("D"); } public static void showExce() throws Exception { throw new Exception(); } } BCD -
**写出程序结果 : class Demo { public static void func() { try { throw new Exception(); System.out.println("A");//此处两句代码位置写反,故不能通过编译 } catch (Exception e) { System.out.println("B"); } } public static void main(String[] args) { try { func(); } catch (Exception e) { System.out.println("C"); } System.out.println("D"); } } 编译错误 -
写出程序结果 : class Exc0 extends Exception { } class Exc1 extends Exc0 { } class Demo { public static void main(String[] args) { try { throw new Exc1(); } catch (Exception e) { System.out.println("Exception"); } catch (Exc0 e) { System.out.println("Exc0"); } } } 编译错误,第2个catch的范围比第一个小 -
class Test { public static String output = ""; public static void foo(int i) { try { if (i == 1) throw new Exception(); output += "1"; } catch (Exception e) { output += "2"; // return; } finally { output += "3"; } output += "4"; } public static void main(String args[]) { foo(0); System.out.println(output);// foo(1); System.out.println(output);// } } 134 134234 Java允许在finally中改变返回值的做法是不好的,因为如果存在finally代码块,try中的return语句不会立马返回调用者,而是记录下返回值待finally代码块执行完毕之后再向调用者返回其值 -
public class ReturnExceptionDemo { static void methodA() { try { System.out.println("进入方法A"); throw new RuntimeException("制造异常"); } finally { System.out.println("用A方法的finally"); } } static int methodB() { try { System.out.println("进入方法B"); // throw new Exception(); return 1; } catch (Exception e) { return 3; } finally { System.out.println("调用B方法的finally"); // return 2; } } public static void main(String[] args) { try { methodA(); } catch (Exception e) { System.out.println(e.getMessage()); } int i = methodB(); System.out.println(i); } } 进入方法A 用A方法的finally 制造异常 进入方法B 调用B方法的finally 1 -
public static void main(String[] args) { int test = test(3,5); System.out.println(test); } public static int test(int x, int y){ int result = x; try{ if(x<0 || y<0){ return 0; } result = x + y; return result; }finally{ result = x - y; } } 8 原因: (1)finally块中的代码是无论try中是否发生异常,也无论catch是否可以捕获异常,也不管try和catch中是否有return语句,都会执行的部分 (2)如果finally中有return语句,那么try...catch...finally结构 一定从finally中的return回去 (3)如果finally中没有return语句,那么try...catch...finally结构才会从try或catch中的return回去,但是finally值中代码不会影响最终的返回值
java
##异常 综合练习1
效果如图:
--------------------------
要计算几个整数的平均值呢:
5
请输入第1个数
50
请输入第2个数
60
请输入第3个数
80
请输入第4个数
90
请输入第5个数
100
一共5个数,和为:380,平均值为:76
----------------------------
要计算几个整数的平均值呢:
3
请输入第1个数
50
请输入第2个数
-15
N必须是正数或者0
请输入第2个数
0
请输入第3个数
-10
N必须是正数或者0
请输入第3个数
60
一共3个数,和为:110,平均值为:36
------------------------------
代码:
import java.util.Scanner;
public class Demo3{
public static void main(String[] args) {
while (true){
System.out.println("----------------------------");
System.out.println("要计算几个整数的平均值呢:");
Scanner sc=new Scanner(System.in);
Averge averge = new Averge();
int n=sc.nextInt();
for (int i=1;i<=n;i++) {
System.out.println("请输入第" + i + "个数");
Scanner sc1 = new Scanner(System.in);
int num = sc1.nextInt();
try {
averge.checkNum(num);
} catch (numException e) {
System.out.println("num必须是正数或者0");
}
}
System.out.println("一共"+n+"个数,和为:"+averge.getSum()+",平均值为:"+(averge.getSum()/n));
}
}}
//自定义异常
public class numException extends Exception{
public numException() {
}
public numException(String message) {
super(message);
}
}
//定义异常功能
public class Averge {
private int sum;
public int checkNum(int num) throws numException{
if (num < 0) {
throw new numException();
}else{
return sum+=num;
}
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
}



