3

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");
}

}

8
  • It only prints out the exception if you a) catch it and print it out or b) don't catch and the default handler prints it out. Jan 8, 2014 at 23:27
  • use recursion alghoritm. Iteration is not "pure" for fibonacci. unsigned int fib(unsigned int n) { if(n == 0) return 0; if(n == 1) return 1; return fib(n-1)+fib(n-2); } Jan 8, 2014 at 23:27
  • 1
    @WojciechSzymski The problem with that is it is O(fib(N)) which is rather like exponential time complexity. Jan 8, 2014 at 23:28
  • 1
    @WojciechSzymski (1) this is beside the point of the problem; (2) see Peter's comment, if you use recursion you will be recomputing fib(N) many times for the same N; (3) there is no value to using recursion in a program to do something that can be done easily using iteration. None. Recursion may look "pure", but that's just means mathematicians will love it. Programmers, not so much. Needless recursion is a cancer. Please refrain from recommending it.
    – ajb
    Jan 8, 2014 at 23:30
  • @Glenndisimo I think your question needs some clarification. You say the method is supposed to throw an exception, but who (if anyone) is supposed to catch it? It's not normal for a method to throw an exception and then catch its own exception, so I don't understand your comment about return values and local variables. If the method throws an exception, the method won't return anything and its local variables become irrelevant.
    – ajb
    Jan 8, 2014 at 23:32

2 Answers 2

5

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.

8
  • Is there also a possibility to keep my local variables? This is how I received the assignment, I don't think I'm allowed to edit them. Jan 8, 2014 at 23:31
  • @Glenndisimo Why do you want to "keep my local variables"? What are you trying to accomplish? I think you must be fundamentally confused about something.
    – ajb
    Jan 8, 2014 at 23:33
  • @ajb since I can't change anything to the assignment except for throwing the exception. Jan 8, 2014 at 23:36
  • @WillBD That's what I originally had and making the return wrong values. Once 1 Exception is thrown the program should stop. Jan 8, 2014 at 23:41
  • 2
    @Glenndisimo I don't think your professor expects the method to be "unchanged" if the exception is thrown. So when you say "I can't change anything to the assignment", I'm pretty sure that doesn't apply to the cases when an exception occurs. If your professor expects the method to throw an exception and return the same result, with the same values for local variables and everything, then your professor is the confused one.
    – ajb
    Jan 8, 2014 at 23:48
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.