0

In a Java assignment I am required to use the throw Exception, but I get a weird error:

Controller.java:13: error: ';' expected

public Controller() throw Exception { ^

The code follows:

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);
  }
  }




  }

The entire syntax seems to be correct, most errors of this type on Stackoverflow are a result of typos, but I don't seem to have any. I edited the smaller mistakes in the code, but I still get the error on the constructor.

Mari
  • 1
  • 1
  • 11
    Classes don't throw exceptions; methods and constructors do. – rgettman Oct 04 '18 at 19:39
  • 2
    Note that `throws Exception` is rarely a good idea to use, even when you put it in the right place (on a method or constructor). Throw a more specific exception type, so that callers can sensibly handle it. In this case, `throws InterruptedException` would be more appropriate. – Andy Turner Oct 04 '18 at 19:42
  • In your code it's the constructor `Controller()` where a `InterruptedException` may occur. So if you do not want to handle that exception the constructor has to throw it, so the caller will have to handle it. Have a look at: [Can constructors throw exceptions in Java?](https://stackoverflow.com/questions/1371369/can-constructors-throw-exceptions-in-java) – LuCio Oct 04 '18 at 19:43
  • Also, you don't mean `i >= 10`, as this loop would then never execute. – Andy Turner Oct 04 '18 at 19:44
  • 1
    Welcome to StackOverflow - besides helping you with your code snippet: people are more likely to read your code and help you, if you format it properly. – Michael Lihs Oct 04 '18 at 19:47
  • Also, you don't need the `.currentThread()`. Sleep is a static method, so you're not invoking it "on that thread" (you could even invoke `((Thread) null).sleep(100)`). – Andy Turner Oct 04 '18 at 19:49

5 Answers5

0

You have three '{' and four '}'. Correct your braces and code shall run smoothly.

nisahbhtrap
  • 92
  • 1
  • 1
  • 8
0

When a method or constructor throw a exception, the caller has to handle or throw it again

So you could do this to throw again

public static void main(String[] args) throws Exception {

or use try catch to handle

try {
    new Controller();
} catch (Exception e) {
    e.printStackTrace();
}
Gnk
  • 645
  • 4
  • 11
0

First of all, it is bad practice to throw java.lang.Exception, as said by @Andy Turner, throw a more specific one (InterruptedException) or create your proper domain exception (by extending java.lang.Exception),as it'll surely make your project more maintainable when it'll become bigger.

When you throw a checked exception (subclasses of java.lang.exception or itself), the callers must either forward it to the next caller as shown below with the throws in front of throwCheckedException() method that forward it to the main() method and so forth:

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);
    }
}

Or catching it and log the cause of the exception for monitoring purposes:

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);
    }
}

BTW another remark:

All classes in the java.lang package are imported by default, thus you don't need to import java.lang.*; to use the Thread class...

isqo
  • 386
  • 4
  • 7
-1

Classes can't throw exceptions.

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);
     }
  }

}
RobOhRob
  • 585
  • 7
  • 17
  • If you say *"Classes can't throw exceptions"*, why does your updated class declare that it `throws Exception`? --- That one reason for down-votes. The other is lack of explanation. – Andreas Oct 04 '18 at 19:51
  • Haha I forgot to remove it when I copy and pasted. – RobOhRob Oct 04 '18 at 19:52
  • Besides that: [Thanks for taking the time to contribute an answer ...](https://stackoverflow.com/help/how-to-answer) – LuCio Oct 04 '18 at 19:56
  • 1
    Because the class doesn't throw the exception, the constructor does. This should be marked as the answer – CoupFlu Oct 04 '18 at 19:56
-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);
    }
}

}

CoupFlu
  • 311
  • 4
  • 20