Java中未处理的异常 [英] Unhandled Exception in Java

查看:422
本文介绍了Java中未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何正确地执行自定义异常,我偶然发现问题。每当我尝试利用抛出此自定义异常的类的对象时,我的IDE的调试器(我使用IntelliJ想法)说未处理的异常:InsertExceptionName()。代码以简化的方式看起来像这样。在这种情况下,如果随机生成的数字为< 0.5,它将返回异常,否则返回数字,但不会这样做。
我缺少什么?

  public class main {
public static void main(String [] args ){
double x = Math.random();
操作op = new operation();
op.execute(x);
}
}

-

  public class operation {
public operation(){
}

public double execute(double x)throws RandomWeirdException {
if(x< 0.5){
throw new RandomWeirdException(<0.5);
}
return x;
}
}

-

  public class RandomWeirdException extends Exception {
public RandomWeirdException(){
super();
}
public RandomWeirdException(String message){
super(message);
}
}


解决方案

你的意思是返回一个例外?当抛出异常时,它会引发调用堆栈。



在这种情况下,您不处理它。它达到 main ,因此你有一个未处理的异常。



如果你想处理一个例外,你会使用 try-catch 块。在这种情况下,最好围绕 main

  try {
//可能抛出
//异常的代码。
} catch(Exception e){
//处理它。
}

另一个解决方案是指定 main 首先引用 RandomWeirdException ,而不是 catch

  public static void main(String [] args)throws RandomWeirdException {...} 
pre>

最好只是让函数 throw ,除非你可以合理地处理异常情况。
如果您只是 catch ,为了捕捉而不会在特殊情况下做任何有意义的事情,这相当于有时候隐藏错误。


I'm currently in the process of learning how to properly do custom exception and I stumbled upon a problem. Whenever I try to utilize an object of a class that throws this custom exception, my IDE's debugger (I'm using IntelliJ idea) says "Unhandled Exception: InsertExceptionName()". The code, in a simplified manner, looks something like this. In this case, it should return an exception if the randomly generated number is <0.5, and return a number otherwise, but it won't do that. What am I missing?

public class main {
    public static void main(String[] args) {
        double x=Math.random();
        operation op=new operation();
        op.execute(x);
   }
}

-

public class operation {
    public operation() {
    }

    public double execute(double x) throws RandomWeirdException {
        if(x<0.5) {
            throw new RandomWeirdException("<0.5");
        }
        return x;
    }
}

-

public class RandomWeirdException extends Exception{
    public RandomWeirdException() {
        super();
    }
    public RandomWeirdException(String message) {
        super(message);
    }
}

解决方案

What do you mean "return" an exception? When an exception is thrown, it bubbles up the call stack.

You are not handling it in this case. It reaches main and thus you have an unhandled exception.

If you want to handle an exception, you'd use a try-catch block. Preferably surrounding main in this case.

try {
    // Code that might throw
    // an exception.
} catch (Exception e) {
    // Handle it.
}

Another solution would be to specify that main throws a "RandomWeirdException", and not catch it in the first place.

public static void main(String[] args) throws RandomWeirdException { ... }

It's preferable to just let functions throw, unless you can reasonably handle the exceptional case. If you just catch for the sake of catching without doing anything meaningful in an exceptional case, it's the equivalent of hiding an error sometimes.

这篇关于Java中未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆