Exceptions In Java
A software engineer may also commit several errors while designing the project or developing the code. These errors are also called 'bugs' and the process of removing them is called 'debugging'. Let us take a look at different types of errors that are possible in a program.
Errors in Java Program
Compile-Time Errors
These are syntactical errors found in the code, due to which a program fails to compile. For example, forgetting a semicolon at the end of a java statement, or writing a statement without proper syntax will result in compile-time error.
Example
// compile-time error
class Err{
public static void main(String[] args) {
System.out.println("Hello")
}
}
The output is
java: ';' expected
In this program, we are not writing a semicolon at the end of the statement. This is detected by java compiler. Detecting and correcting compile-time errors is easy as the java compiler displays the list of errors with the line numbers along with their description.
Run-Time Errors
These errors represent inefficiency of the computer system to execute a particular statement. For example, insufficient memory to store something or inability of the microprocessor to execute some statements come under run-time errors.
Example
// run-time error
class Err{
public static void main() {
System.out.println("Hello");
}
}
The output is
Error: Main method not found in class Err, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Run-time errors are not detected by the java compiler. They are detected by the JVM, only at runtime.
Logical Errors
These errors depict flaws in the logic of the program. The programmer might be using a wrong formula or the design of the program itself is wrong. This error is not detected by JVM and compiler.
Example
// logical error
class Err{
public static void main(String[] args) {
double sal = 5000.00;
sal = sal* 15/100; // wrong logic - sal += sal*15/100;
System.out.println("Incremented salary ="+ sal);
}
}
The output is
Incremented salary =750.0
In the above program due to use of wrong code or logic the output is also wrong.
Exceptions
Generally, an exception is a runtime error. All exceptions occur only at runtime but some exception are detected at compile time and some other at runtime. The exceptions that are checked at compilation time by the java compiler are called checked exceptions
while the exceptions that are checked by JVM are called unchecked exceptions
.
Types Of Exception
Built-In Exception
Built-in exception are the exceptions which are already available in java. These exceptions are suitable to explain certain error situations.
- ArithmeticException - It is thrown when an exceptional condition has occurred in an arithmetic operation.
- ArrayIndexoutOfBounndsException - Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
- ClassNotFoundException - This exception is raised when we try to access a class whose definition is not found.
- FileNotFoundException - Raised when a file is not accessible or does not open
- IOException - Thrown when an input-output operation failed or interrupted
- InterruptedException - Thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted
- NoSuchFieldException - Thrown when a class does not contain the field (or variable) specified.
- NoSuchMethodException - Thrown when accessing a method which is not found.
- NullPointerException - Raised when referring to the members of a null object.
- NumberFormatException - Raised when a method could not convert a string into a numeric format.
- RunTimeException - This represents any exception which occurs during runtime.
- StringIndexOutOfBoundsException - Thrown by string class methods to indicate that an index is either negative or greater than the size of the string.
User-Defined Exception
Sometimes, the built-in exceptions in java are not able to describe a certain situation. In such cases, like built-in exceptions, the user or programmer can also create his or her own exceptions which are called user-defined exceptions
.