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

31st Aug 2020, 5:00 PM
pNK
pNK - avatar
6 Réponses
+ 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
31st Aug 2020, 5:20 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
~ swim ~ Thank you for the answer now it makes sense :)
31st Aug 2020, 5:20 PM
pNK
pNK - avatar
+ 1
🇮🇳Omkar🕉 Does this have to do with the fact that it's a runtime exception rather that a checked exception?
31st Aug 2020, 5:23 PM
pNK
pNK - avatar
+ 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.
31st Aug 2020, 5:38 PM
pNK
pNK - avatar