0
Java Exceptions
Why is it that sometimes in java I have to import the Exception class while other times it don't? Example: public static int display(int a, int b) throws ArithmeticException, IOException{ throw new ArithmeticException("Cannot divide by zero"); } here I have to import java.io.IOException but there is not a import for ArithmeticException
6 Answers
+ 5
`ArithmeticException` is defined in `java.lang` package. `java.lang` is auto imported.
Not only `ArithmeticException` but other classes like `Integer` , `Thread`, `Math` are in `java.lang` package. So every time you need to use these classes you don't need to explicitly use imports.
https://devdocs.io/openjdk~8/java/lang/package-summary
+ 1
~ swim ~ Thank you for the answer now it makes sense :)
+ 1
š®š³Omkarš Does this have to do with the fact that it's a runtime exception rather that a checked exception?
+ 1
Thanks š®š³Omkarš and ~ swim ~ I understand why now. I was stuck on this question for quite some time š
all the other online sources didn't really explain why.