0

I have a silly doubt. While using throws I know we specify multiple exceptions. My query is can we include Exception (generic) also along with other specific exceptions? Is that right coding approach?

public String display() throws IOException, FileNotFoundException, Exception {
    ...
}

Here using Exception is right approach??

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
  • If the exception is not happening in the code then why `throws` the exception. – Hamza Anis Jun 01 '17 at 12:43
  • 1. If you declare that your method throws a checked exception (something that extends `Exception` rather than `RuntimeException`), your force the caller to handle it. They need to write the try-catch even if they might not need one, which I would say is a bit silly (I really don't want to go into debate whether checked exceptions are bad or not). 2. If you force the caller to handle it, but it is the most generic exception possible - what do you want them to handle? – Jaroslaw Pawlak Jun 01 '17 at 12:44
  • I really wanted the caller to handle whatever the method throws! only point is apart from specific exceptions if i also throw generic exception what is the difference? – Balachandar Thangavel Jun 01 '17 at 13:57

3 Answers3

4

It is legal code.

It is a bad idea.

The reason that it is a bad idea is that the caller now has to handle (or propagate) Exception. But an Exception could be almost anything. How can you do anything sensible1 with an exception in an exception handler if you don't know what it could be?

I would argue that if you don't know what the cause of an exception is, the only totally safe thing you can do is to log it and terminate the application. In some contexts you could try to log the exception, "fail" the request and carry on with the next one. That may be a sensible strategy, if service availability is the highest priority, and the system is designed to be robust (e.g. idempotent requests, transactional implementation). However, there is a risk that this approach will increase the damage. But the problem with throwing Exception is that you make it much harder to discriminate between errors that are recoverable and those that are not.

Graham
  • 7,431
  • 18
  • 59
  • 84
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Wow! That explains!! got the point why Sonar keeps telling me to remove generic exception! – Balachandar Thangavel Jun 01 '17 at 13:59
  • one more silly doubt! considering am not including the generic "Exception" in throws? Will it be right approach to catch the Exception inside the method. I understand I ask naive questions. Since it never throws compiler error, I wanted to adhere to coding standards for better code quality! – Balachandar Thangavel Jun 01 '17 at 14:01
  • Go to the Documentation example page I linked to and look at the other examples in the same topic. There is one that talks about catching `Exception`. – Stephen C Jun 01 '17 at 14:03
2

It is legal Java code.

First part for the usage of Exception in throws declaration.

It should seldom be written in production code, before it suggests that the method could throw absolutely any checked exception, which is quite uncommon.

But it is currently used in Junit tests. The aim of a test function is to test one single feature, and should be as simple as possible because it is expected to not contain logic errors. So it is common to write the test as:

@Test
public void testFeature() throws Exception {
    //actual test that can call any function declaring checked exception
}

Here this just means that you do not want to worry for possible exception declared in functions used in the test body.


But your code declares Exception in addition to other exception (sub-)classes. That is useless because all exception classes are subclasses of Exception, thus IOException and FileNotFoundException are Exceptions.

So even if legal, this does not make sense: if you put Exception in throws declaration, you should remove all other exception classes because they are already contained in the Exception declaration.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

In my opinion method can throw generic Exception, when you business logic and your pipeline works like application has to continue work even after fatal error, log this error as FFDC and continue works. If your application has to shutdown after fatal eror, then your method has not to throw generic Exception.

dikkini
  • 1,184
  • 1
  • 23
  • 52