在千锋“逆战”学习第26天
每日一句:其实你找不到错误不代表错误不存在,同样你看不到技术比你牛的人并不代表世界上没有技术比你牛的人。
今天为Map集合收尾,主要讲异常的知识
明天继续加油。
作业
1.填空
--------------------------------------------------------------------------
2.查询API,填空
--------------------------------------------------------------------------
3.下列代码补充完整
public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n) {
if (n == 0) {
// 抛出一个NullPointerException
throw new NullPointerException();
} else {
// 抛出一个ClassCastException
// 并设定详细信息为“类型转换出错”
throw new ClassCastException("类型转换出错");
}
}
}
运行结果:
--------------------------------------------------------------------------
4.有如下代码
public class TestException {
public static void main(String[] args) {
System.out.println("main 1");
int n;
// 读入n
Scanner input = new Scanner(System.in);
System.out.println("输入n的值:");
n = input.nextInt();
ma(n);
System.out.println("main 2");
}
public static void ma(int n) {
try {
System.out.println("ma1");
mb(n);
System.out.println("ma2");
} catch (EOFException e) {
System.out.println("Catch EOFException");
} catch (IOException e) {
System.out.println("Catch IOException");
} catch (SQLException e) {
System.out.println("Catch SQLException");
} catch (Exception e) {
System.out.println("Catch Exception");
} finally {
System.out.println("in finally");
}
}
public static void mb(int n) throws Exception {
System.out.println("mb1");
if (n == 1)
throw new EOFException();
if (n == 2)
throw new IOException();
if (n == 3)
throw new SQLException();
if (n == 4)
throw new NullPointerException();
System.out.println("mb2");
}
}
当读入n分别为1、2、3、4、5时,输出结果是什么
main 1
输入n的值:
1
ma1
mb1
Catch EOFException
in finally
main 2
main 1
输入n的值:
2
ma1
mb1
Catch IOException
in finally
main 2
main 1
输入n的值:
3
ma1
mb1
Catch SQLException
in finally
main 2
main 1
输入n的值:
4
ma1
mb1
Catch Exception
in finally
main 2
main 1
输入n的值:
5
ma1
mb1
mb2
ma2
in finally
main 2
--------------------------------------------------------------------------
7.代码改错
class MyException{}
public class TestException {
public static void main(String[] args) {
ma();
}
public static int ma() {
try {
m();
return 100;
} catch (Exception e) {
System.out.println("Exception");
}catch (ArithmeticException e) {
System.out.println("ArithmeticException");
}
}
public static void m() {
throw new MyException();
}
}
1.先catch ArithmeticException然后catch Exception
2.m方法只能抛出Throwable类下的异常
--------------------------------------------------------------------------
9.有如下代码:
public class TestTryCatch {
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma() {
int n ;
try {
n=10/0;
} catch (Exception e) {
}
return n;
}
}
运行结果:
A.编译不通过。try执行不到的时候,n将未初始化,编译不通过
--------------------------------------------------------------------------
10.有如下代码:
public class TestFinally {
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma() {
int b;
Scanner sc = new Scanner(System.in);
System.out.println("输入b");
b = sc.nextInt();
try {
int n = 100;
return n / b;
} catch (Exception e) {
return 10;
} finally {
return 100;
}
}
}
运行结果:
当读入b为100时,输出结果为:100
当读入b为0时,输出结果为:100
--------------------------------------------------------------------------
11.有如下代码:
public class TestTryFinally {
public static void main(String[] args) {
try {
ma();
} catch (Exception ex1) {
}
}
public static void ma() throws Exception {
int n = 10;
int b;
Scanner sc = new Scanner(System.in);
System.out.println("输入b");
b = sc.nextInt();
try {
System.out.println("ma1");
int result = n / b;
System.out.println("ma2 " + result);
} finally {
System.out.println("In Finally");
}
}
}
运行结果:
当读入b为10时,输出结果为:
ma1
ma2 1
In Finally
当读入b为0时,输出结果为:
ma1
In Finally
--------------------------------------------------------------------------
13.有如下代码:
public class TestException {
public static void main(String[] args) {
try {
System.out.println("main1");
ma();
System.out.println("main2");
} catch (Exception e) {
System.out.println("In Catch");
}
}
public static void ma() {
System.out.println("ma1");
throw new NullPointerException();
System.out.println("ma2");
}
}
运行结果:
编译出错,抛出异常后面的输出语句执行不到
--------------------------------------------------------------------------
14.
public class TestException {
public static void main(String[] args) {
try {
ma();
}
/*1*/
catch (Exception e) {
}
}
public static void ma() throws IOException{}
}
下列哪些代码放在/1/处可以编译通过
catch(NullPointerException npe) {}//可以
catch(IOException ioe) {}//可以
catch(SQLException sqle) {}//不可以,无法访问SQLException的catch块。此异常从不从try语句体引发