第九章
package cn.lyvust;
/**第一题
* 2021-5-6
* 任静
*/
public class Student {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(){}
}
package cn.lyvust;
public class TestNull {
public static void main(String[] args) {
try{
Student stu=null;
stu.name="Mike";
stu.age=20;
System.out.println("学员姓名"+stu.getName()+",年龄"+stu.age);
}catch(NullPointerException ex){ //处理异常
System.out.println("空指针异常");
}
}
}
空指针异常
进程已结束,退出代码为 0
package cn.lyvust;
/**第二题
* 2021-5-6
* 任静
*/
public class ScoreException extends Exception{
public ScoreException(String message) {
super(message);
}
}
package cn.lyvust;
import java.util.Scanner;
public class Test {
public void inputScore()throws ScoreException{
Scanner input = new Scanner(System.in);
int score=input.nextInt();
if(score>=0 && score<=100){
System.out.println(score);
}else{
throw new ScoreException("请正确输入成绩信息");
}
}
public static void main(String[] args){
Test test=new Test();
try{
test.inputScore();
}catch(ScoreException se){
System.out.println(se.getMessage());
}
}
}
98
98
进程已结束,退出代码为 0