基本异常实践问题
basic exception practice issue
上传错误截图
这是一个显示基本 ArithmeticException 在 netbeans 中给我错误的示例。有任何想法吗。
public class Exception {
public static void main (String[] args){
try
{
double x = 0;
double y = 19000;
double z;
double practice()
{
double z = 4000;
return y - z;
}
public double practiceAgain()
{
double f = (9 + z);
return f/x;
}
}
catch (ArithmeticExecption t){
System.out.println(t);
}
}
如错误所述:
Uncompilable source code – illegal start of expression at Exception:main(Exception.java:13)
您的源代码无效,原因有多种:
- 方法内部没有范围之类的东西(即
public
、protected
或 private
)——您的错误消息中提到了这一点:您的第 13 行main
方法。
- 在一个方法中,您不能像这样声明另一个方法(即第 18 和 24 行)
基本上有两种方法可以使发布的代码可编译:
要么将内容移出main
方法:
public class Exception {
double x = 0;
double y = 19000;
double z;
public static void main (String[] args) {
try {
practice();
practiceAgain();
} catch (ArithmeticExecption t){
t.printStackTrace();
}
}
double practice() {
z = 4000;
return y - z;
}
public double practiceAgain() {
double f = (9 + z);
return f/x;
}
}
或者直接在main
方法中进行处理:
public class Exception {
public static void main (String[] args) {
try {
double x = 0;
double y = 19000;
double z = 4000;
double practice = y - z;
double f = (9 + z);
double practiceAgain = f/x;
} catch (ArithmeticExecption t){
t.printStackTrace();
}
}
}
上传错误截图
这是一个显示基本 ArithmeticException 在 netbeans 中给我错误的示例。有任何想法吗。
public class Exception {
public static void main (String[] args){
try
{
double x = 0;
double y = 19000;
double z;
double practice()
{
double z = 4000;
return y - z;
}
public double practiceAgain()
{
double f = (9 + z);
return f/x;
}
}
catch (ArithmeticExecption t){
System.out.println(t);
}
}
如错误所述:
Uncompilable source code – illegal start of expression at Exception:main(Exception.java:13)
您的源代码无效,原因有多种:
- 方法内部没有范围之类的东西(即
public
、protected
或private
)——您的错误消息中提到了这一点:您的第 13 行main
方法。 - 在一个方法中,您不能像这样声明另一个方法(即第 18 和 24 行)
基本上有两种方法可以使发布的代码可编译:
要么将内容移出main
方法:
public class Exception {
double x = 0;
double y = 19000;
double z;
public static void main (String[] args) {
try {
practice();
practiceAgain();
} catch (ArithmeticExecption t){
t.printStackTrace();
}
}
double practice() {
z = 4000;
return y - z;
}
public double practiceAgain() {
double f = (9 + z);
return f/x;
}
}
或者直接在main
方法中进行处理:
public class Exception {
public static void main (String[] args) {
try {
double x = 0;
double y = 19000;
double z = 4000;
double practice = y - z;
double f = (9 + z);
double practiceAgain = f/x;
} catch (ArithmeticExecption t){
t.printStackTrace();
}
}
}