1. 写出程序结果 :
class Demo {
public static void main(String[] args) {
try {
showExce();//异常 转到catch
System.out.println("A");
} catch (Exception e) {
System.out.println("B");
} finally {//怎么都要执行
System.out.println("C");
}//try catch之外,直接运行
System.out.println("D");
}
public static void showExce() throws Exception {
throw new Exception();
}
}
运行结果: B C D
2. **写出程序结果 :
class Demo {
public static void func() {
try {
throw new Exception();//把下一行删除后 异常 进入func()方法里的catch输出B
System.out.println("A");//会报错 删除后正常编译运行
} catch (Exception e) {
System.out.println("B");
}
}
public static void main(String[] args) {
try {
func();//调用func方法 B 无异常
} catch (Exception e) {
System.out.println("C");
}
System.out.println("D");
}
}
运行结果: 无法成功运行,将func()方法里System.out.println(“A”);删除后可以运行。
3. 写出程序结果 :
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");//Exception 是Exc0的父类,会报错。
}
}
}
运行结果: 无法成功运行,异常捕获 catch 的异常类 父类不能在子类之前
4.
class Test {
public static String output = "";
public static void foo(int i) {
try {
if (i == 1)
throw new Exception();//if (i == 1){throw new Exception();} 此刻i ==0 不满足 继续执行下面
output += "1";//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 throw new Exception是if条件语句内容,这里没有大括号而已,因为不满足if条件所以执行output、finally的output和最后的output。
再输出134 234 因为前面的output已经有134 了再运行foo(1)是向output里面添加字符
5.
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();//"进入方法A";"用A方法的finally"
} catch (Exception e) {//methodA()的异常没处理被捕获
System.out.println(e.getMessage());//"制造异常"
}
int i = methodB();//进入方法B" "调用B方法的finally"
System.out.println(i//1
}
}
运行结果:进入方法A 用A方法的finally 制造异常 进入方法B 调用B方法的finally
6.
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;//这里的返回值是x+y
}finally{//不影响上面的返回值
result = x - y;
}
}
运行结果:8
异常 综合练习1
/*
编写一个计算N个整数平均值的程序。程序应该提示用户输入N的值,如何必须输入所有N个数。如果用户输入的值是一个负数,则应该抛出一个异常并捕获,提示“N必须是正数或者0”。并提示用户再次输入该数(自定义异常)
*/
效果如图:
--------------------------
要计算几个整数的平均值呢:
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
------------------------------
参考代码
public class MyException extends Exception{
public MyException(String message){//异常信息
super(message);
}
}
public class Avg {
public static void main(String[] args) throws Exception {
double avg = 0;
Scanner sc = new Scanner(System.in);
boolean flag = true;
while(flag) {
System.out.println("--------------------------------");
System.out.println("要计算几个整数的平均值呢:");
int N = sc.nextInt();
double scort = 0;
for (int i=1;i<=N;i++){
System.out.println("请输入第"+i+"个数");
double num = sc.nextDouble();
if (num<0){//用try catch来捕获异常;
try{
throw new MyException("N必须是正数或者0");//声明自定义异常
}
catch(Exception e){//捕获异常输出异常信息
System.out.println(e.getMessage());
} i--;//有异常i-- 重新输入这一轮
continue;//结束本次循环开始下次循环避免执行循环体后续代码
}
//循环的主要目的是总分的相加
scort +=num;
if(i==N){
flag = false;
}//输入全部数后退出循环
}//平均数的计算和输出
avg = scort / N;
System.out.println("一共"+N+"个数,和为:"+scort+",平均值为:"+avg);
}
}
}