Console Errors in Java
An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is avoided from being compiled or run due to several mistakes.As a result, mistakes ought to be constant before compilation and execution. The famous maximum errors may be comprehensively named as follows:
1. Error at Run Time:
During the path of this system's execution, Run Time mistakes are discovered.When a person enters information that is only sometimes applicable or is invalid, those can every so often be found.Runtime mistakes arise whenthe software asks the pc to do somethingit cannot reliably do regardless of now no longer having any syntax mistakes.
The compiler no longer has a manner of discovery and discovery like those in the pilot while his system runs; its miles detect the JVM (Java Virtual Machine).We can position our mistake code within the attempted lock and capture the mistake within the capture block to address the mistake during the runtime.
For instance: A runtime mistake will arise if the person enters information withinside the string layout while the pc is watching for an integer. E.g. Error at runtime added on through dividing through zero.
Example 1:
class MisspelledVar {
public static void main(String args[])
{
int a = 40, b = 60;
// Declared variable Sum with Capital S
int Sum = a + b;
// Trying to call variable Sum
// with a small s i.e. sum
System.out.println(
"Sum of variables is "
+ sum); }
}
Output:
prog.java:14: error: cannot find symbol
+ sum);
^
symbol: variable sum
location: class MisspelledVar
1 error
Example 2:
class PrintingSentence {
public static void main(String args[])
{
String s = "GeeksforGeeks";
// Missing ';' at the end
System.out.println("Welcome to " + s)
}
}
Output:
prog.java:8: error: ';' expected
System.out.println("Welcome to " + s)
1 error
Example 3:
class MissingParenthesis {
public static void main(String args[]) {
System.out.println("Printing 1 to 5 \n");
int i;
// missing parenthesis, should have
// been for(i=1; i<=5; i++)
for (i = 1; i<= 5; i++ {
System.out.println(i + "\n"); } } }
Output:
prog.java:10: error: ')' expected
for (i = 1; i<= 5; i++ {
1 error
2. Logical error:
A compile-time error is an error that prevents code from executing because of incorrect syntax. For example, missing parentheses, classes, semicolons at the end of statements, etc. During compilation, these errors are flagged by the Java compiler, and error messages are displayed on the screen. A syntax error is another name for a compilation error.
The Java compiler catches these errors so you can easily find and fix them. The compiler tells you which component of your program has problems and guesses what went wrong. If the problem is caused by improper nesting of parentheses, the actual problem may be at the beginning of the block. The compiler will usually give you the exact line where the error occurred or possibly the line immediately preceding it. Syntax errors are grammatical mistakes that occur when using a programming language
public class LErrorDemo {
public static void main(String[] args)
{
int num = 789;
int reversednum = 0;
int remainder;
while (num != 0) {
// to obtain modulus % sign should
// have been used instead of /
remainder = num / 10;
reversednum= reversednum * 10+ remainder;
num /= 10;
}
System.out.println("Reversed number is "+ reversednum);
}
}
Output:
Reversed number is 7870
class IncorrectMessage {
public static void main(String args[])
{
int a = 2, b = 8, c = 6;
System.out.println(
"Finding the largest number \n");
if (a > b && a > c)
System.out.println(a + " is the largest Number");
else if (b > a && b > c)
System.out.println(b + " is the smallest Number");
// The correct message should have
// been System.out.println
//(b+" is the largest Number");
// to make logic
else
System.out.println(
c + " is the largest Number");
}
Output:
Finding the largest number
8 is the smallest Number
3. Syntax error:
Programmers face syntax and logic errors.
A misspelling or grammatical error is a syntax error. B. Use of uninitialized variables, use of undefined variables, missing semicolons, etc
Syntax errors can be removed by the help of compiler.