Catching an exception and returning a custom message

exception, java, try-catch

Solution

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.

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:

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.

Problem

So I'm making some school assignments and I have to throw an Exception if the condition between my "if" statement is met. ``` 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? Cause if if I use try-catch blocks it keeps getting problems with my return values because the assignment used local variables. The program should stop after throwing 1 Exception Thanks in advance! EDIT: As requested my custom Exception ``` public class FibonacciException extends ArithmeticException { public FibonacciException() { super(); System.out.println("Max value surpassed"); } ``` }

Original source