Java 这里的新手,不知道在哪里使用试抓块

Java 这里的新手,不知道在哪里使用试抓块,java,Java,我有3个java程序 第一个: public class Person{ private String firstname; private String lastname; private int g1; private int g2; private int g3; private int periodic; public Person(String firstname,String lastname,int periodic,i

我有3个java程序

第一个:

public class Person{
    private String firstname;
    private String lastname;
    private int g1;
    private int g2;
    private int g3;
    private int periodic;



    public Person(String firstname,String lastname,int periodic,int g1,int g2,int g3){
        this.firstname = firstname;
        this.lastname = lastname;
        this.periodic=periodic;
        this.g1=g1;
        this.g2=g2;
        this.g3=g3;
    }

    public void print(){

        int a=(g1+g2+g3)/3;
        System.out.println("\t"+ firstname + " "+ lastname);
        System.out.println("Periodic Exam: " + periodic);
        System.out.println("Grade of quiz 1: "+g1);
        System.out.println("Grade of quiz 2: "+g2);
        System.out.println("Grade of quiz 3: "+g3);
        System.out.println("Average of the quizzes: "+a);
    }


}
第二个:

public class Student extends Person{  //inherits Person//

    public Student(String firstname,String lastname,int periodic,int g1,int g2,int g3){
        super(firstname,lastname,periodic,g1,g2,g3);    
    }

    public void print(){
        System.out.println("Student Details:");
        super.print();

    }
}
第三个:

public class Main{
    public static void main(String[] args){

        Person persons[] = new Person[3];
        persons[0] = new Student("Robert","Almazan",91,90,85,86);
        persons[1] = new Student("Dennis","Navan",92,89,84,87);
        persons[2] = new Student("Allan","Villariza",93,79,81,84);

        for(Person person:persons){
            person.print();

        }
}
我所要做的就是在像这样更改persons[]时捕获异常

Person persons[] = new Person[3];
        persons[0] = new Student("Robert","Almazan","ZZ",0,-85,86);
        persons[1] = new Student("Dennis","Navan","ASA",0,-84,87);
        persons[2] = new Student("Allan","Villariza","sa",0,-81,84);
public class Main{
    public static void main(String[] args){


    try{

        Person persons[] = new Person[3];
        persons[0] = new Student("Robert","Almazan","ZZ",90,85,86);
        persons[1] = new Student("Dennis","Navan","ASA",89,84,87);
        persons[2] = new Student("Allan","Villariza","9",79,81,84);

        for(Person person:persons){
            person.print();



        }
    }//try
    catch(ArithmeticException e){

        System.out.println("Arithmetic Error");
    }

    catch(InputMismatchException e){

        System.out.println("Input Error");
    }
    finally{

        System.out.println("Thank you");
    }


    }//main
}
我试过这样做

Person persons[] = new Person[3];
        persons[0] = new Student("Robert","Almazan","ZZ",0,-85,86);
        persons[1] = new Student("Dennis","Navan","ASA",0,-84,87);
        persons[2] = new Student("Allan","Villariza","sa",0,-81,84);
public class Main{
    public static void main(String[] args){


    try{

        Person persons[] = new Person[3];
        persons[0] = new Student("Robert","Almazan","ZZ",90,85,86);
        persons[1] = new Student("Dennis","Navan","ASA",89,84,87);
        persons[2] = new Student("Allan","Villariza","9",79,81,84);

        for(Person person:persons){
            person.print();



        }
    }//try
    catch(ArithmeticException e){

        System.out.println("Arithmetic Error");
    }

    catch(InputMismatchException e){

        System.out.println("Input Error");
    }
    finally{

        System.out.println("Thank you");
    }


    }//main
}
程序不捕获异常

当我编译时,它仍然说像字符串这样的错误不能转换为int,它总是接受负数,并且它不捕获异常

需要帮助…

您的代码:

persons[0] = new Student("Robert","Almazan","ZZ",0,-85,86); 
                                            ^^^^
是编译时错误,因为您接受的第三个参数是int类型

public Student(String firstname,String lastname,int periodic,int g1,int g2,int g3){
                                                ^^^^^^^^^^^
您只能在运行时捕获异常,而不能在编译时捕获异常。

当我编译时,它仍然说像字符串这样的错误无法转换为 int,它总是接受负数,并且不捕获异常

首先,异常发生在运行时而不是编译时,因为您的程序有一个编译时错误,这是不言自明的,即
字符串不能转换为int

问题是

new Student("Robert","Almazan","ZZ",0,-85,86);
                                /\
                                ||
                              This is String not an int
但是你的学生类构造函数是这样的

public Student(String firstname,String lastname,int periodic,int g1,int g2,int g3)
                                                      /\
                                                      ||
                                                    int not String

这就是为什么会出现编译时错误,现在你的程序甚至还没有成功编译,所以在你的程序运行之前不可能出现错误。

我的Java已经生锈了,但不应该是'Person[]=new Person[3];`是“Person[]persons=新人[3];`@据我所知,宋楚瑜和宋楚瑜都可以。Zer0:也许你应该试着区分哪些是独立的问题:“它”是谁?编译器?你有什么例外?为什么你期望负数不会被接受?我认为它应该是学生类的构造函数而不是Person类。