Table of Contents

Java Errors

The error indicates a problem that mainly occurs due to the lack of system resources and our application should not catch these types of problems. Some of the examples of errors are system crash error and out of memory error. Errors mostly occur at runtime that's they belong to an unchecked type. It is therefore important to detect and manage properly all the possible error conditions in the program so that the program will not terminate or crash during execution.

Types of Errors:

Errors may broadly classified into two categories:

i) Compile-time errors

ii) Run -time errors

i) Compile-time errors:

All syntax errors will be detected and displayed by the java compiler and therefore these errors are known as compile-time errors. This compiler error indicates something that must be fixed before the code can be compiled.

Most frequent Compile-Time errors are:

  • Missing Parenthesis ( } )
  • Printing the value of variable without declaring it
  • Missing semicolon (terminator)
  • Misspelling of identifiers and keywords
  • Missing of double quotes in strings.

Example:

class CompiletimeError {
 public static void main(String args[]) {
   System.out.println("Hello Java") // ; missing
 }
}
/*OUTPUT:
   CompiletimeError.java:4:37: error: ';' expected
   System.out.println("Hello Java") // ; missing */

ii) Run -time errors:

Sometimes, a program may compile successfully creating the .class file but not run properly. Such programs may produce wrong results due to wrong logic and may terminate due to errors such as stack overflow.

Most frequent Run -time errors are:

  • Dividing an integer by zero
  • Accessing an element that is out of the bounds of an array
  • Passing a parameter that is not in a valid range or value for a method
  • Converting invalid string to a number.

Example:

class RuntimeError {
 public static void main(String args[]) {
   int a = 10;
   int b = 5;
   int c = 5;
   int x = a / (b - c);
   System.out.println("x = " + x);
   int y = a / (b + c);
   System.out.println("y = " + y);
 }
}

/*OUTPUT:

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?