简体   繁体   English

捕获异常并返回自定义消息

[英]Catching an exception and returning a custom message

So I'm making some school assignments and I have to throw an Exception if the condition between my "if" statement is met. 所以我正在做一些学校作业,如果满足我的“if”语句之间的条件,我必须抛出异常。

public class Fibonacci {
private static final long MAX = 91;

public static long finonacciGetal(int n) {
        if (n > MAX || n < 0) throw new FibonacciException();
        else {

            long eerste = 0;
            long tweede = 1;
            long getal = 0;

            for (int i = 0; i < n; i++) {
                getal = eerste + tweede;
                eerste = tweede;
                tweede = getal;
            }
            return getal;
    }
}

} }

Now I made a custom Exception where it returns an error message but it still keeps printing out the stacktrace. 现在我做了一个自定义异常,它返回一条错误消息,但它仍然保持打印出栈跟踪。 So is there a way to hide it from the Exception class itself? 那么有没有办法将它从Exception类本身隐藏起来? Cause if if I use try-catch blocks it keeps getting problems with my return values because the assignment used local variables. 因为如果我使用try-catch块它会不断出现返回值的问题,因为赋值使用了局部变量。 The program should stop after throwing 1 Exception 该程序应该在抛出1个异常后停止

Thanks in advance! 提前致谢!

EDIT: As requested my custom Exception 编辑:根据要求我的自定义例外

public class FibonacciException extends ArithmeticException {
public FibonacciException() {
    super();
    System.out.println("Max value surpassed");
}

} }

The trick with this is indeed to use a try catch block, and because you mentioned that the variables were all local, you might just have to put them outside of the try catch block. 这样做的确是使用try catch块,并且因为你提到变量都是本地的,你可能只需将它们放在try catch块之外。

EDIT 编辑

So, now that I understand the problem a little bit more detailed-ly. 所以,现在我对这个问题有了更详细的了解。 I think I understand where the confusion is coming from. 我想我明白混乱的来源。 You are told to throw an exception if you get to an iteration that is above your max, which is a fine way to do it, but now you need a way to deal with this exception item. 如果你得到一个高于你的最大值的迭代,你会被告知抛出一个异常,这是一个很好的方法,但现在你需要一种方法来处理这个异常项。

So, lets take your original code: 所以,让我们拿你原来的代码:

public class Fibonacci {
private static final long MAX = 91;

public static long finonacciGetal(int n) {
        if (n > MAX || n < 0) throw new FibonacciException();
        else {

            long eerste = 0;
            long tweede = 1;
            long getal = 0;

            for (int i = 0; i < n; i++) {
                getal = eerste + tweede;
                eerste = tweede;
                tweede = getal;
            }
            return getal;
    }
}
}

This is fine as is, really. 这很好,真的。 Now, if you look at the case that throws the exception, none of the values in your local variables are computed yet, this is good because, this exception means that someone tried to use this method with a value that was outside the range of what you were allowing. 现在,如果你看一下抛出异常的情况,你的局部变量中没有任何值被计算出来,这很好,因为这个异常意味着有人试图使用这个方法的值超出范围你允许的。 One way to make SURE the person who is using this class is dealing with your exception is to add a throws clause to the method declaration, like so: 确保正在使用此类的人处理异常的一种方法是在方法声明中添加throws子句,如下所示:

public class Fibonacci {
private static final long MAX = 91;

public static long finonacciGetal(int n) throws FibonacciException {
        if (n > MAX || n < 0) throw new FibonacciException();
        else {

            long eerste = 0;
            long tweede = 1;
            long getal = 0;

            for (int i = 0; i < n; i++) {
                getal = eerste + tweede;
                eerste = tweede;
                tweede = getal;
            }
            return getal;
    }
}
}

That way, when someone goes to use it (in say, main) like so: 这样,当有人去使用它(例如,主要)时,如此:

public static void main(String[] args) {
    try{
        System.out.println(new Fibonacci().fibonacciGetal(92));
    }catch(FibonacciException e){
        System.out.println(e.getMessage());
    }
}

You'll notice that you have to use a try/catch in the method that is USING it, which is the proper way to handle these situations. 你会注意到你必须在使用它的方法中使用try / catch,这是处理这些情况的正确方法。

Just update your exception so that the message is the message: 只需更新您的异常,以便消息是消息:

public class FibonacciException
extends ArithmeticException {

    public FibonacciException() {
        super("Max value surpassed");
    }
}

The caller of your method will do something like this: 您的方法的调用者将执行以下操作:

try {
    long f = finonacciGetal(-1);

} catch(FibonacciException fe) {

    // prints something like
    // "com.acme.FibonacciException: Max value surpassed"

    System.err.println(fe); 
}

Right now it prints the stack trace automatically because you aren't catching it. 现在它会自动打印堆栈跟踪,因为您没有捕获它。 When you catch an exception you get to decide what to do about it instead and that is up to the caller of your method. 当您捕获异常时,您可以决定该怎么做,而这取决于您的方法的调用者。 You don't have to worry about that part. 你不必担心那部分。 Your assignment is just to throw the exception. 你的任务只是抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM