(1)修改下面的程序,使程序能够编译运行,而且在myMethod与main方法中都声明抛出而不处理异常。↵ public class Quiz {↵ public static void main(String[]args){↵ myMethod();↵ } ↵ myMethod() { ↵ throw new MyException();↵ }↵ } ↵ class MyException {↵ public String toString(){↵ return“自定义异常”; }↵ (2)修改程序,在myMethod方法中声明抛出而不处理异常,在main方法中处理异常。↵ (3)修改程序,在myMethod方法中处理异常,在main方法中不需要考虑异常处理。 首生
时间: 2023-11-27 10:52:59 浏览: 82
(1) 修改后的程序如下:
```
public class Quiz {
public static void main(String[] args) throws MyException {
myMethod();
}
public static void myMethod() throws MyException {
throw new MyException();
}
}
class MyException extends Exception {
public String toString() {
return "自定义异常";
}
}
```
(2) 修改后的程序如下:
```
public class Quiz {
public static void main(String[] args) {
try {
myMethod();
} catch (MyException e) {
System.out.println(e);
}
}
public static void myMethod() throws MyException {
throw new MyException();
}
}
class MyException extends Exception {
public String toString() {
return "自定义异常";
}
}
```
(3) 修改后的程序如下:
```
public class Quiz {
public static void main(String[] args) {
myMethod();
}
public static void myMethod() {
try {
throw new MyException();
} catch (MyException e) {
System.out.println(e);
}
}
}
class MyException extends Exception {
public String toString() {
return "自定义异常";
}
}
```
阅读全文