Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Introduction of Errors
Java Exceptions
Java Try Block
Java Catch Block
Java Finally Block
Java Throw Keyword
Java Throws Keyword
Java Builtin Exceptions
User Defined Exceptions

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Errors


Errors in Java Compile Time and Run Time Errors

What is error?

Errors are the mistakes that can make a program to go wrong in execution. They may be a logical or typical mistakes.

An error may produce an incorrect output or may terminate the program from execution, sometimes it causes to crash the system also.

So it is very important to detect and manage the errors in the program so that system will not crash during while running on the system.

Types of errors

Generally errors are classified into two main categories as

  1. Compile time errors
  2. Run time errors

1. Compile time errors

Compile time errors are the syntax errors. These errors are mainly occurs due to the missing statements or writting wrong program without considering it's syntax.

The main reasons for this type of errors are poor understanding of the language.

Compile time errors must be fixed before running the program. Java doesn't create .class file if the program contains errors.

Example of compile time error

Following program contains errors which are said to be compile time.

class CompileErrors
{
   public static void main(String args[])
   {
      String str = "Hello Androidberry" ; // missing double quote
      System.out.println(str)   // missing ;
   }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
String literal is not properly closed by a double-quote

In above program there is a missing double quote error. We must fix above error to proceed the further compilation process.Thats why we will not get missing semicolon error.

List of common compile time errors

  1. Class extending more than one class.
  2. Missing semicolon in the statement.
  3. Bad referrence to objects.
  4. Declaration of same name of - class,field,interface,method etc.
  5. Undefined class due to missing of package.
  6. Missing double quotes in the String.
  7. Using undeclared variables (fields) in the program.
  8. Missing brackets of - class,method,interface etc.
  9. Trying to create object of abstract class.
  10. Redefining final methods, changing value of final fields.

2. Run time Errors

Run time errors are the logical errors. Sometimes the program successfully compiles and creates the .class file but displayes the incorrect output which is not expected by programmer.

These type of error are occure due to the incorrect logic or poor understanding of the problem statement.

Sometimes the wrong logical program may crash the system by producing mistakes like overflowing of stack, infinite running of loops, allocating resources etc

Example of Run time error

Following example displayes the addition of array elements

class RuntimeError
{
    public static void main(String args[])
    {
       int array []={1,2,3,4,5};
       int add=0;
       for(int i=1;i<5;i++) // wrong start of index
       {
	   add = add + array[i];
       }
       System.out.println("Addition : "+add) ;
    }
}

Output

Addition : 14

In above example we are getting output as 14 but expected is 15. The indexing of array always starts from 0 hence it results in the output but doesn't give any error.

List of commong Run time errors

  1. Converting invalid string into integer.
  2. Wrong start of array index.
  3. Dividing the number by zero.
  4. Accessing the out of index element from array size.
  5. Run out of memory error.
  6. Accessing a character that is out of bounds of a string.
  7. Trying to illegally change the state of a thread.
  8. Trying to cast an instance of a class to one of it's subclass.
  9. Using a negative size for an array.
  10. Using null object reference to access methods or variables.
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo