1

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.

  • 1
    Not sure how to answer this in a general way. – Dave Newton Jun 23 '15 at 15:47
  • Can you please show some code snippets? – Chetan Kinger Jun 23 '15 at 15:50
  • 1
    I would discourage what you are trying, but if you really want this, there is a way: just **always** put `throws Exception` after every method signature. (e.g. `public void myMethod(String param) throws Exception { ... }`). But basically this means that your entire application will shut down whenever your program has an Exception. – bvdb Jun 23 '15 at 15:51
  • I'd strongly discourage `throws Exception`. It will hide the fact that you actually need to handle specific exceptions in specific ways (e.g. `InterruptedException`), which your methods may not throw now, but might do as your code evolves. – Andy Turner Jun 23 '15 at 15:53
  • You mentioned that the user needs to input an int, is that from using a `Scanner`? – matrixanomaly Jun 23 '15 at 15:56
  • @matrixanomaly exactly! – Laurenzanoster Jun 23 '15 at 16:06
  • 1
    @Laurenzanoster see my answer for this case then! You're on the right track, but you can handle this without exceptions. – matrixanomaly Jun 23 '15 at 16:29

4 Answers4

2

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

Community
  • 1
  • 1
Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
2

Using Java 7 you can do it.

Refer Can I catch multiple Java exceptions in the same catch clause?

Working with Java SE 7 Exception Changes

Community
  • 1
  • 1
Ashish Nijai
  • 321
  • 2
  • 13
1

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.

dilix
  • 3,761
  • 3
  • 31
  • 55
1

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.

Community
  • 1
  • 1
matrixanomaly
  • 6,627
  • 2
  • 35
  • 58
  • 1
    Thank you very much for your time @matrixanomaly, i will definitely try this as its simpler. – Laurenzanoster Jun 23 '15 at 18:06
  • @Laurenzanoster no worries, we're all here to help! I was a TA for a java course in data structures so this is something I've had to teach students! Best of luck with java and welcome to SO! – matrixanomaly Jun 23 '15 at 18:12