二、实验内容
(a、Define Course class,Course contains private String cno, private String cname,private int credit, Course contains methods: public Course( ), public Course (String cno, String cname, int credit ).
Define CreditException class extends (继承) from RunTimeException, CreditException contains methods: public CreditException( ), public CreditException(String Message).
Construction method of Course will throw a CreditException if Course’s credit is below 0.5 or above 6 , Message is” credit should between 0.5 and 6! ”.
Define public class TestCourse, Create a Course object, and use try/catch block to handle the CreditException.
定义课程类,课程包含私有字符串cno,私有字符串cname,私有int学分,课程包含方法:公共课程(),公共课程(字符串cno,字符串cname,int学分)。
定义 CreditException 类扩展 (继承) 从 RunTimeException,CreditException 包含的方法:public CreditException( )、public CreditException(String Message)。
构造方法 当然,如果课程的学分低于 0.5 或高于 6,则 Credit 将抛出一个 CreditException,消息是“信用应该在 0.5 到 6 之间!".
定义公共类 TestCourse,创建一个 Course 对象,并使用 try/catch 块来处理 CreditException。
(b、Define Employee class,Employee contains private int eno, private String ename,private int eage, Employee contains methods: public Employee ( ), public Employee (int eno, String ename, int eage).
Define EnoException class extends (继承) from RunTimeException, EnoException contains methods: public EnoException ( ), public EnoException (String Message).
Construction method of Employee will throw an EnoException if Employee’s eno is below 20 or above 60 , Message is” Eno should between 20 and 60! ”.
Define TestEmployee class, Create an Employee object, and use try/catch block to handle the EnoException.
定义 Employee 类,Employee 包含 private int eno、private String ename、private int eage、Employee 包含方法:public Employee ( )、public Employee (int eno、String ename、int eage)。
定义EnoException类扩展(继承)从RunTimeException,EnoException包含方法:public EnoException( ),public EnoException(String Message)。
如果员工的 eno 低于 20 或高于 60,员工的构造方法将抛出 EnoException,消息为“Eno 应该在 20 到 60 之间!".
定义 TestEmployee 类,创建一个 Employee 对象,并使用 try/catch 块来处理 EnoException。
三、实验步骤
1.
package course1;
public class course {
private String cno,cname;
private int credit;
course(){
}
course(String cno,String cname,int credit)throws outofcreditE{
if(credit<0.5||credit>6)throw new outofcreditE("学分应该在0.5~6");
this.cname=cname;
this.cno = cno;
this.credit = credit;
}
}
package course1;
@SuppressWarnings("serial")
public class outofcreditE extends RuntimeException{
public outofcreditE(String string){
super(string);
}
}
package course1;
public class Main {
public static void main(String[] args){
try {
course c = new course("001","java",9);
}catch(outofcreditE e){
System.out.println("e: "+e.getMessage());
}
}
}
2.
package pEmployee;
public class TestEmployee {
public static void main(String[] args) {
try {
Employee employee = new Employee(15, "John", 30);
} catch (EnoException e) {
System.out.println("EnoException: " + e.getMessage());
}
}
}
package pEmployee;
public class EnoException extends RuntimeException {
public EnoException() {
super();
}
public EnoException(String message) {
super(message);
}
}
package pEmployee;
public class TestEmployee {
public static void main(String[] args) {
try {
Employee employee = new Employee(15, "John", 30);
} catch (EnoException e) {
System.out.println("EnoException: " + e.getMessage());
}
}
}
四、实验结果
五、实验小结
在这个解决方案中,我们创建了两个自定义的异常类CreditException和EnoException,它们都继承自RuntimeException类。在Course类和Employee类的构造方法中,分别通过条件判断抛出了CreditException和EnoException。接着我们使用try/catch块捕获了这两个异常,并对其进行了处理。
通过这个示例,我们展示了如何创建自定义的异常类以及如何在构造方法中抛出异常,并使用try/catch块来处理异常。这样可以使程序在出现特定情况时抛出自定义的异常,从而更好地管理错误的情况