捕获异常
public class test {
public static void main(String [] args) {
int[] arr= {
1,2,3,4,5
};
try {
System.out.println(arr[10]);
}catch (ArrayIndexOutOfBoundsException e) {//索引越界异常
System.out.println("索引越界");
}
System.out.println("看看我是否执行");
}
}
异常的常见方法
1.getMessage()
public class test {
public static void main(String [] args) {
int[] arr= {
1,2,3,4,5
};
try {
System.out.println(arr[10]);
}catch (ArrayIndexOutOfBoundsException e) {//索引越界异常
String message=e.getMessage();
System.out.println(message);
}
System.out.println("看看我是否执行");
}
}
2.toString()
public class test {
public static void main(String [] args) {
int[] arr= {
1,2,3,4,5
};
try {
System.out.println(arr[10]);
}catch (ArrayIndexOutOfBoundsException e) {//索引越界异常
String str=e.toString();
System.out.println(str);
}
System.out.println("看看我是否执行");
}
}
3.printStackTrace()
public class test {
public static void main(String [] args) {
int[] arr= {
1,2,3,4,5
};
try {
System.out.println(arr[10]);
}catch (ArrayIndexOutOfBoundsException e) {//索引越界异常
e.printStackTrace();
}
System.out.println("看看我是否执行");
}
}
抛出异常
throw:
public class test {
public static void main(String [] args) {
int[] arr= null;
int max=getmax(arr);
System.out.println(max);
}
public static int getmax(int [] arr) {
if(arr==null) {
throw new NullPointerException();//此方法结束,下面代码不会执行
}
System.out.println("看看我执行了没");
int max=arr[0];
for(int i=0;i<arr.length;i++) {
if(max<arr[i]) {
max=arr[i];
}
}
return max;
}
}
throws:
public class test {
public static void main(String [] args) {
int[] arr= null;
int max=0;
try {
max=getmax(arr);
}catch (NullPointerException e) {
System.out.println("空指针异常");
}
System.out.println(max);
}
public static int getmax(int [] arr)throws NullPointerException {
if(arr==null) {
throw new NullPointerException();//此方法结束,下面代码不会执行
}
System.out.println("看看我执行了没");
int max=arr[0];
for(int i=0;i<arr.length;i++) {
if(max<arr[i]) {
max=arr[i];
}
}
return max;
}
}
综合练习
键盘录入数据需求:键盘录入自己心仪的女朋友姓名和年龄。姓名的长度在3~10之间,年龄的范围为18- 40岁,超出这个范围是异常数据不能赋值,需要重新录入,一直录到正确为止。
package test02;
import javax.management.RuntimeErrorException;
public class Girl {
private String name;
private int age;
public Girl() {
}
public Girl(String name,int age) {
this.setName(name);
this.setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
int len=name.length();
if(len<3||len>10) {
throw new RuntimeException();
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age<18||age>40) {
throw new RuntimeException();
}
this.age = age;
}
@Override
public String toString() {
return "Girl [name=" + name + ", age=" + age + "]";
}
}
public class test {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
Girl girl=new Girl();//创建对象
while(true) {
try {
System.out.println("请输入姓名:");
String name=sc.next();
girl.setName(name);
//年龄
System.out.println("请输入年龄:");
int age=sc.nextInt();
girl.setAge(age);
break;
} catch (RuntimeException e) {
System.out.println("姓名或年龄范围有误");
}
}
System.out.println(girl);
}
}
自定义异常
1)定义异常类
2)写继承关系
3)空参构造
4)带参构造
package test02;
public class NameFormatException extends RuntimeException{
public NameFormatException() {
}
public NameFormatException(String message) {
super(message);
}
}
package test02;
public class AgeBoundException extends RuntimeException {
public AgeBoundException() {
}
public AgeBoundException(String message) {
super(message);
}
}
package test02;
import javax.management.RuntimeErrorException;
public class Girl {
private String name;
private int age;
public Girl() {
}
public Girl(String name,int age) {
this.setName(name);
this.setAge(age);
}
public String getName() {
return name;
}
public void setName(String name) {
int len=name.length();
if(len<3||len>10) {
throw new NameFormatException(name+"格式有误应该为3-10");
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if(age<18||age>40) {
throw new AgeBoundException(age+"格式有误超出合理范围");
}
this.age = age;
}
@Override
public String toString() {
return "Girl [name=" + name + ", age=" + age + "]";
}
}
public class test {
public static void main(String [] args) {
Scanner sc=new Scanner(System.in);
Girl girl=new Girl();//创建对象
while(true) {
try {
System.out.println("请输入姓名:");
String name=sc.next();
girl.setName(name);
//年龄
System.out.println("请输入年龄:");
int age=sc.nextInt();
girl.setAge(age);
break;
} catch (NameFormatException e) {
e.printStackTrace();
}catch (AgeBoundException e) {
e.printStackTrace();
}
}
System.out.println(girl);
}
}