1

I try to throw an IllegalFormatException().

throw new IllegalFormatException();

but I get this compile error The constructor IllegalFormatException() is not visible. what did I do is wrong?

Se Song
  • 1,613
  • 2
  • 19
  • 32
  • 1
    @Sotirios Delimanolis ...I don't know if my question is duplicated, but IllegalFormatException is not my class. So I can't modify the package as the question you marked as duplicated question. please answer the question. I already read the that question, but I didn't got the answers – Se Song Nov 20 '15 at 02:13
  • 1
    The answer for the exception you specified is that the constructor is not visible. If you look at the implementation of the class, the constructor is package private. You can't access it. That's all. – Sotirios Delimanolis Nov 20 '15 at 02:14
  • ok, thank you... but I don't know how to fix it – Se Song Nov 20 '15 at 02:16
  • 1
    There is no way to fix it. Use a different exception. – Sotirios Delimanolis Nov 20 '15 at 02:16
  • so it mean that no ways I can throw new IllegalFormatException() in java? – Se Song Nov 20 '15 at 02:20
  • With that syntax, you can't. You can use reflection, but don't. – Sotirios Delimanolis Nov 20 '15 at 02:21
  • This exception type is not meant to be used directly. – Sotirios Delimanolis Nov 20 '15 at 02:21
  • if your marked as duplicated doesn't help to answers, and the answers is completely difference, please don't mark as duplicate – Se Song Nov 20 '15 at 02:28
  • You'll note the text "This question already has an answer here." above your post. You asked _what did I do is wrong?_ You'll then note the accepted answer in the linked post states _Another class from another package, however, cannot access any of the constructors._ That's exactly what is happening here. Your class is in a different package than `IllegalFormatException` and therefore doesn't have access to its constructor. That is considered a duplicate. Don't take it badly. – Sotirios Delimanolis Nov 20 '15 at 02:31
  • As the javadoc says: "Unchecked exception thrown when a format string contains an illegal syntax or a format specifier that is incompatible with the given arguments. Only explicit subtypes of this exception which correspond to specific errors should be instantiated." You can find more information about itand the name of the sub-classes that you can instantiate in he docs: https://docs.oracle.com/javase/8/docs/api/index.html?java/util/IllegalFormatException.html – Rodrigo Bagni Feb 28 '19 at 20:36

0 Answers0