0
votes

im trying to throw exception from method,but when i compile the below code

class launcher{
public static void main(String[] args){
    try{
        getError();
        System.out.println("Line: try block");
    }catch(myException e){
        System.out.println("Line: catch block");
    }finally{
        System.out.println("Line: finally block");
    }
    System.out.println("Line: EOF main");
}
static void getError() throws myException{
        throw new myException();
    }
}

compile error

launcher.java:14: error: cannot find symbol static void getError() throws ^myException{

symbol: class myException

location: class launcher

it says something, it can't understand what is myException

1
And have you written myException.java and does it extend Exception?Elliott Frisch
@ElliottFrisch nomasternone
you can not throw not existing Exception.dieter
im following book of java v5 ,they written like above code "scaryException,pantsException" without writing "scaryException.java", i thought "throws myException" creates subclass of Exception classmasternone
replace myException by RuntimeException.dieter

1 Answers

1
votes

Problem reason and solution

The problem is, that you haven't imported your Exception to the launcher class. Exceptions are classes so need to be declared as a typical class.

Huge problem

You start class names with lowercase letters which makes your class non-readable. Your classes should be called: Launcher (or better Test) and MyException instead of myException.

You can create your own Exceptions. The following is a declaration of MyException which you might find helpful:

MyException

public class MyException extends RuntimeException {
    public MyException() {
        super();
    }
}

Then import your new class and you'll be able to throw it.