Laurenzanoster
Laurenzanoster

Reputation: 117

Java - Write one method to handle all exceptions

im trying to handle exceptions on my code and at the same time without repeating it.

I have lots of method on the "System" Class and i would prefer not to write "Try Catch" in all of them as i believe that there is some other way not to repeat it.

Any Ideas?

The exception that i want to handle is (InputMismatchException).

Edit: More info just in case you need it. I have several methods where the User needs to input 'Int'(using Scanner), thats what i need to handle.

Upvotes: 1

Views: 2445

Answers (4)

matrixanomaly
matrixanomaly

Reputation: 6947

This is a different approach you want to try, that does not deal with Exceptions implicitly.

As mentioned in your comments you're taking input from the user through the Scanner class. Since the Scanner allows the user to type in any value they desire and you only want to deal with Integers, you probably want to (or you're using already) the Scanner.nextInt() method. Tutorial here.

If you just need to handle the InputMismatchException, this question has an answer that handles all that exception in one place.

Alternatively, as this question explores, you could do away with having to deal with Exceptions by just always asking the user for an int until the user properly enters an integer, modified below:

Scanner sc = new Scanner(System.in); //your scanner instance
System.out.print("Enter a number.");
while (!sc.hasNextInt()) {
        System.out.println("Enter a whole number");
        sc.next();
}
int x = sc.nextInt();

What the while loop in there does is always repeat whenever the scanner does not have a next integer, hence the !sc.hasNextInt() condition, and prompt the user for another number with the message and the sc.next(), which is a method that waits for the user to input again.

Once the user enters a number, the hasNextInt() method will return a true, which makes the while loop condition false (since we did a !hasNextInt() ) where ! gives the other Boolean value, and the assigned x will always be an int, hence avoiding exception handling.

This is good because you don't have to deal with a ton of exceptions and try-catch blocks, and avoid having to do the discouraged methods as others have mentioned, and I'm presuming this is for a simple program or some assignment, avoiding the extra complexity.

However if you need more advanced error handling exceptions are definitely something you should look into, so you're on the right track of thought.

Upvotes: 1

Liviu Stirb
Liviu Stirb

Reputation: 6075

I don't generally recommend this: Just make all your methods to throw exception and treat all the exceptions at a upper level in one place. This is good only is for some tests or when you want to rapid develop something and shouldn't be the normal approach.

You can find more here: https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

Also some rules regarding how should you do it: Throws or try+catch

Upvotes: 2

dilix
dilix

Reputation: 3893

It's not a good idea to have one god class that handles all exceptions. Usually each exception is a unique situation that should be handled separately.

But if you still want to, you could add throws declaration to each of your method and then in one start point catch them all.

Upvotes: 1

Related Questions