Java Exceptions | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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
4 Respostas
+ 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