Errors and Exceptions in Java
Before we begin understanding what errors and exceptions are, it’s important to first understand how programming languages work and how computers detect errors. A computer is a machine that follows instructions written by us, but it doesn’t have intelligence of its own. So how does it know when something in our code is wrong? Let’s start from there.
We all know that a computer is a dumb and deaf machine—it cannot think or act on its own. It fully depends on the user’s instructions. For example, if you’re a designer, the computer behaves like a designing tool. If you’re a gamer, it becomes a gaming machine. Similarly, if you’re a programmer, it behaves like a programming tool that follows your commands.
But here’s something interesting to think about:
As a programmer, you write instructions (code) based on your understanding. You may believe your code is correct, but then—suddenly—the computer/compiler throws an error and says something is wrong. Now the question is: How can a computer know that there’s something wrong in your instructions?
The answer lies in how programming languages work.
When you write a program, it needs to be converted into machine language (low-level language) so that the computer can understand and execute it. This process is done by a special software called a compiler (or interpreter, depending on the language).
If the compiler is unable to translate your code due to incorrect syntax, missing symbols, wrong structure, or other violations of the language rules, it throws an error.
So basically, the computer doesn’t “know” anything on its own. It’s just following the strict rules of the programming language. When your code doesn’t follow these rules, the compiler flags it as an error.
Now, from the above we can say that “In programming, errors refer to problems or bugs in your code that prevent it from running correctly or as expected. These errors can occur during writing, compiling, or running the program.”
Types of Errors
Compile Time Error :
All syntax errors will be detected and displayed by the Java compiler and therefore these errors are known as compile time error. Whenever the compiler displays an error, it will not create the .class file. It is therefore necessary to fix the error before we can successfully compile and run the program.
System.out.println("Hello World" // missing closing parenthesis
Most of the compile-time errors are:
- Missing semicolons
- Missing bracket in class and method
- Misspelling of identifier & keywords
- Missing double quotes in the string
- Use of undeclared variables
- Incompatible types in assignment / initialization
- Bad reference to objects
- Use of = in place of == operator
Run Time Errors
Sometimes, a program may compile successfully, creating the .class file, but may not run properly. Such programs may produce wrong results due to wrong logic or may terminate due to errors such as stack overflow.
The most common run-time errors are:
- Dividing an integer by zero
- Accessing an element that is out of the bounds of an array
- Trying to store a value into an array of incompatible class or type
- Passing a parameter that is not in a valid range or value for a method
- Trying to illegally change the state of a thread
- Attempting to use a negative size for an array
- Using a null object reference as a legitimate object reference to access a method or a variable
- Converting an invalid string to a number
- Accessing a character that is out of bounds of a string
Exceptions
An exception in Java is an unexpected event that happens during the execution of a program (at runtime), which disrupts the normal flow of instructions.
But the key point is: Exceptions are not just problems — they are problems that can be handled.
Now, from the above definition, you might get confused and wonder:
How is an exception different from a runtime error?
Think of it this way:
-
Runtime errors are like fatal accidents — your program crashes, and there’s not much you can do.
-
Exceptions are like potholes on the road — your program hits one, but if you’re prepared (using
try-catch
), you can handle it and continue the journey.
Exception vs Errors
Types of Exceptions :
Checked Exceptions (Compile-Time Exceptions)
- These are exceptions that are checked at compile time.
- If your code might cause such an exception, the compiler forces you to handle it using try-catch or throws.
- If you don’t handle them, your program won’t compile.
- Examples:
IOException, SQLException, FileNotFoundException, ClassNotFoundException - Let’s understand the given code :
import java.io.*; public class Example { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("file.txt"); // Might throw FileNotFoundException int data = fr.read(); } }
Unchecked Exceptions (Runtime Exceptions)
- These are exceptions that are not checked at compile time but occur at runtime.
- You are not forced to handle them, but you can handle them if you want.
- Examples:
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException - Let’s understand the given code :
public class Example { public static void main(String[] args) { int a = 10 / 0; // ArithmeticException } }
Summary
I hope you now have a clear understanding of the basic concepts of Errors and Exceptions in Java, along with their types and key differences. Understanding these fundamentals is important before diving into real-world coding scenarios.
In the next article, we’ll take this knowledge a step further and explore how to handle exceptions in Java using try, catch, finally, and throw/throws, with lots of practical examples to make learning easier and more effective. Stay tuned!