我试图从方法抛出异常,但当我编译下面的代码
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(); } }编译错误
launcher.java:14:错误:找不到符号static void getError()throws ^ myException {
symbol:类myException
位置:类启动器
它说了些什么,它无法理解什么是myException
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
最满意答案
问题原因和解决方案
问题是,您没有将Exception导入launcher类。 Exception是class es,因此需要声明为典型类。
大问题
您使用小写字母启动类名称,这使您的类不可读。 应该调用您的类: Launcher (或更好的Test )和MyException而不是myException 。
您可以创建自己的例外。 以下是MyException的声明,您可能会发现它有用:
MyException
public class MyException extends RuntimeException { public MyException() { super(); } }然后导入你的新课程,你就可以抛出它。
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.
更多推荐
发布评论