Exceptions
An exception is an object that’s created when an error occurs in a Java program, and Java can’t automatically fix the error. The exception object contains information about the type of error that occurred. The most important information — the cause of the error — is indicated by the name of the exception class used to create the exception. You usually don’t have to do anything with an exception object other than figure out which one you have.
A different exception class represents each type of exception that can occur. Here are some typical exceptions:
IllegalArgumentException
: You passed an incorrect argument to a method.
InputMismatchException
: The console input doesn’t match the data type expected by a method of the Scanner class.
ArithmeticException
: You tried an illegal type of arithmetic operation, such as dividing an integer by 0 (zero).
IOException
: A method that performs I/O (such as reading or writing a file) encountered an unrecoverable I/O error.
ClassNotFoundException ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access