java throw异常找不到符号(java throw exception cannot find symbol)

编程入门 行业动态 更新时间:2024-10-24 15:22:56
java throw异常找不到符号(java throw exception cannot find symbol)

我试图从方法抛出异常,但当我编译下面的代码

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.

更多推荐

本文发布于:2023-08-07 20:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   符号   异常   throw   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!