在Java作业中,我需要使用throw Exception,但是会收到一个奇怪的错误:
Controller.java:13: error: ';' expected
public Controller()引发异常{ ^
代码如下:
import java.awt.*;
import java.lang.*;
public class Controller {
private Canvas can;
private Arrow arr;
public static void main(String[] args){
new Controller();
}
public Controller() throws Exception {
can = new Canvas("Animation", 300, 700);
arr = new Arrow(can);
arr.draw();
Thread.currentThread().sleep(100);
for (int i = 1; i <= 10; i++){
arr.erase();
arr.moveUp();
arr.draw();
Thread.currentThread().sleep(100);
}
}
}
整个语法似乎是正确的,Stackoverflow上这种类型的大多数错误都是由于拼写错误造成的,但是我似乎没有任何错误。 我在代码中编辑了较小的错误,但仍然在构造函数上收到了错误。
答案 0 :(得分:0)
您有三个'{'和四个'}'。更正括号,代码应可平稳运行。
答案 1 :(得分:0)
当方法或构造函数引发异常时,调用者必须处理或再次引发异常
因此您可以执行此操作以再次抛出
public static void main(String[] args) throws Exception {
或使用try catch处理
try {
new Controller();
} catch (Exception e) {
e.printStackTrace();
}
答案 2 :(得分:0)
首先,按照@Andy Turner的说法,抛出java.lang.Exception,抛出更具体的异常(InterruptedException)或创建适当的域异常(通过扩展java.lang.Exception)是一种不好的做法,因为它肯定会使您的项目在变得更大时更加可维护。
当您引发已检查的异常(java.lang.exception或其本身的子类)时,调用者必须将其转发给下一个调用者,如下所示,并且throwCheckedException()之前将其转发到main()方法的方法,依此类推:
public class HelloWorld {
public static void main(String... args) throws SpecificExcpetion {
throwCheckedException();
}
public static void throwCheckedException() throws SpecificExcpetion {
throw new SpecificExcpetion("i'm a specific exception");
}
}
class SpecificExcpetion extends Exception {
public SpecificExcpetion(String message) {
super(message);
}
}
或者捕获它并记录异常原因以进行监视:
public class HelloWorld {
public static void main(String... args){
try {
throwCheckedException();
} catch (SpecificExcpetion specificExcpetion) {
// log the exception with a logger
}
}
public static void throwCheckedException() throws SpecificExcpetion {
throw new SpecificExcpetion("i'm a specific exception");
}
}
class SpecificExcpetion extends Exception {
public SpecificExcpetion(String message) {
super(message);
}
}
顺便说一句:
java.lang包中的所有类都是默认导入的,因此您不需要导入java.lang。*;。使用Thread类...
答案 3 :(得分:-1)
类不能抛出异常。
import java.awt.*;
import java.lang.*;
public class Controller {
private Canvas can;
private Arrow arr;
public static void main(String[] args){
try {
new Controller();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public Controller() throws InterruptedException {
can = new Canvas("Animation", 300, 700);
arr = new Arrow(can);
arr.draw();
Thread.currentThread().sleep(100);
for (int i = 1; i >= 10; i++){
arr.erase();
arr.moveUp();
arr.draw();
Thread.currentThread().sleep(100);
}
}
}
答案 4 :(得分:-1)
public class Controller {
private Canvas can;
private Arrow arr;
public Controller() {
//Option b, public Controller throws Exception{
can = new Canvas("Animation", 300, 700);
arr = new Arrow(can);
//Instead of Try/catch, you can have the constructor "throws Exception"
try{
doingStuff();
}catch(Exception e){
//handle your exception
}
}
private void doingStuff() throws Exception{
arr.draw();
Thread.currentThread().sleep(100);
for (int i = 1; i >= 10; i++) {
arr.erase();
arr.moveUp();
arr.draw();
Thread.currentThread().sleep(100);
}
}
}