Let us now talk about the kinds of errors you can make while writing code.
Let us now try to see the two different kinds of errors which might creep into your programs. One such error is known as the compile time error. So compile time errors are those errors which hinder the compilation of your file. This actually means that the file itself would not compile if you have any compile time errors. So the basic process of running a file is basically the file first compiles and then it runs. So if the file fails to compile in the first step itself, it is an example of a compile time error. As an example, consider that I have declared a marks error like this, which consists of marks of three students. Now here, if I try to write a print statement like this and perhaps if I forget to add a semicolon, then you would see there is a curly red line which appears and which indicates that there is an error. Here it shows that a semicolon is expected. Suppose I don't correct this mistake and I try to run this file.
Then you can see that there is an error which is getting thrown here. It shows that error occurred while compiling this particular file. So this basically is an example of a compile time error which occurs even before the file can be compiled because of some syntax error or other issues. So this is an example of a compile time error. Other examples of compile time error might be that if there is an extra bracket here, then also the file wouldn't compile because the syntax is not correct. So usually all the examples of syntax errors come under the category of compile time errors. Another type of error which might occur is known as the runtime error. So basically, runtime errors do not show up when the file is compiled, but they show up when the file is actually run and executed. So it is not possible to find runtime errors before even beginning to run the file. So basically, runtime errors are much harder to figure out as compared to compile time errors. As an example here, consider that I have a marks array which consists of three elements. But here I am trying to print the element at index number four. Clearly there is no index four which exists inside this array. So this is an invalid statement. But you will see that there is no red curly line which is appearing here, which basically means that the IDE is not able to figure out that this is an invalid statement. So when you run this file, you would see that there is an array out of bounds exception which is thrown. So this exception or this error is actually indicated when the file is run. So this example is of a runtime error. This is an invalid statement because here I am trying to access an element which does not even exist inside the array. However, there is no SYNT tax problem in this statement. And thus it does not show up during the compile time. But an error is thrown only after the file is run and executed.
There are two types of errors that can occur in programs: compile-time errors and runtime errors.
Compile-time errors hinder the compilation of a file, meaning that the file itself will not compile if there are any compile-time errors.
Syntax errors, such as missing semicolons or extra brackets, fall under the category of compile-time errors.
Runtime errors occur when a file is executed and are much harder to figure out than compile-time errors.
Invalid statements that don't have syntax problems can lead to runtime errors, such as trying to access an element that does not exist inside an array.
Runtime errors are only indicated when the file is run and executed.
A run-time error will only occur when the code is actually running. These errors are the most difficult as they lead to unexpected program crashes and bugs in your code, both of which can be hard to track down and fix.
However, syntax errors are compile-time errors. Compile-time errors occur when codes are written in ways in which the Java compiler does not recognise. The compiler will display errors to alert you about something that will not compile and, therefore, cannot be run.
You can refer to this link to read more about these errors.
Let us try to understand how we can fix these errors or “debug” the code.
You now know how to debug a code.
You can see the step-by-step execution of the code by running it in debug mode; you can also see where you made a logical error or an error of another kind, which is preventing you from getting the desired output. In fact, a bug is a particular case of a run-time error.