P116
1.不对。比如定义类属性未使用,也会显示异常,但是能够正确执行。
2.自定义异常类getageException
import java.util.Scanner;
class getageException extends Exception{
public int age;
public getageException(int a){
this.age=a;
System.out.println("age beyond limit(120):"+a);
}
}
public class sevenone {
public static void main(String[] args) throws getageException {
// TODO 自动生成的方法存根
Scanner input=new Scanner(System.in);
final int a;
a=input.nextInt();
if(a>120) {
throw new getageException(a);
}
else {
System.out.println("sb");
}
}
}
3.NullPointerExpection:空指针异常
原因如下:
(1)字符串变量未初始化;
(2)接口类型的对象没有具体的类初始化,例如 有pig类,pig p;会报错,pig p=new pig();不会报错。
(3)若一个对象为空,但没有判断其为空的条件,例如 if(pig!=NULL){...};会报错,if(pig==NULL);if(pig!=NULL){...};正确。
4.输出结果:
exception000 exception111 finished
删除斜体代码后:
exception111 exception finished
5.输出结果:
13423
8.
import java.util.*;
public class Test {
public static int success;
public static int connect(){
Scanner input=new Scanner(System.in);
int x=input.nextInt();
return x;
}
public static void a() throws Exception{
success=connect();
if(success==-1) {
throw new Exception();
}
}
public static void main(String[] args) throws Exception {
try {
a();
}
catch (Exception e){
System.out.println("-1 wrong");
}
finally {
System.out.println("end");
}
}
}
6
7
论述题:
1.
Error类和Exception类是Throwable类的两个直接子类
Error类包括一些严重的程序不能处理的系统能够错误类
如内存溢出,虚拟机错误,栈溢出等
这类错误一般与硬件有关,与程序本身无关,通常由系统进行处理‘
程序本身无法捕获和处理
Exception类
有些异常在编写程序时无法预料,如中断异常,非法存取异常
为了保证程序的健壮性,必须对这些可能出现的异常进行捕获,并对其进行处理
2.
-Checked exception: 继承自 Exception 类。代码需要处理 API 抛出的 checked exception,要么用 catch 语句,要么直接用 throws 语句抛出去。可以理解为编译时异常。
Unchecked exception: 也称 RuntimeException,它也是继承自 Exception。但所有 RuntimeException 的子类都有个特点,就是代码不需要处理它们的异常也能通过编译,所以它们称作 unchecked exception。RuntimeException(运行时异常)不需要try...catch...或throws 机制去处理的异常。运行时异常。
3.
StackOverflowError 是一个java中常出现的错误:在jvm运行时的数据区域中有一个java虚拟机栈,当执行java方法时会进行压栈弹栈的操作。在栈中会保存局部变量,操作数栈,方法出口等等。jvm规定了栈的最大深度,当执行时栈的深度大于了规定的深度,就会抛出StackOverflowError错误。
如果 Java 虚拟机栈大小可以动态扩容,发生扩容时发现内存不足,或者新建Java 虚拟机栈时发现内存不足,抛出 OutOfMemoryError。
4.
异常处理两种方式:声明抛出处理,程序捕获处理。
5.RunExceptionError五个子类
port java.lang.*;
class sb{
String name;
public sb() {
}
}
public class rtexception extends RuntimeException{
public static void emptystack() {
sb x=new sb();
try {
System.out.println(x.name);
}
catch(NullPointerException e){
System.out.println("emptystack error");
}
}
public static void nullpointer() {
int[] a = null;
try {
System.out.println(a[0]);
}
catch(NullPointerException e) {
System.out.println("空指针");
}
}
public static void ariexception() {
int a1=1,b1=0,c1;
c1=a1/b1;
try {
System.out.println(c1);
}
catch(ArithmeticException e) {
System.out.println("divide zero");
}
}
public static void AIOFBexception() {
int [] a=new int[2];
try {
System.out.println(a[3]);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("数组越界");
}
}
public static void Narraysize() {
int []a=new int[10];
try {
System.out.println(a[-1]);
}
catch(NegativeArraySizeException e) {
System.out.println("负下标");
}
}
public static void main(String[] args) {
emptystack();//空栈
nullpointer();//空指针
ariexception();//运算
AIOFBexception();//数组下标越界
Narraysize();//数组负长度
}
}
6.自定义异常类,判断输入年龄是否合理。
import java.util.Scanner;
class getageException extends Exception{
public int age;
public getageException(int a){
this.age=a;
System.out.println("age beyond limit(120):"+a);
}
}
public class sevenone {
public static void main(String[] args) throws getageException {
// TODO 自动生成的方法存根
Scanner input=new Scanner(System.in);
final int a;
a=input.nextInt();
if(a>120) {
throw new getageException(a);
}
else {
System.out.println("sb");
}
}
}
7.
throw:
表示方法内抛出某种异常对象
如果异常对象是非 RuntimeException 则需要在方法申明时加上该异常的抛出 即需要加上 throws 语句 或者 在方法体内 try catch 处理该异常,否则编译报错
执行到 throw 语句则后面的语句块不再执行
throws:
方法的定义上使用 throws 表示这个方法可能抛出某种异常
需要由方法的调用者进行异常处理
8.
finally作用:try语句执行后,无论有没有捕捉到异常(不论有无catch语句),均会执行finally语句。



