Write about Different Types of Errors Occured in Java?

Types of Errors :

 Errors may be classified in to 2 categories

1)Compile time errors

2)Run time errors

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. Whenever the compiles displays an error it will not create the class file. It is therefore necessary that we fix all the errors before we can successfully compile and run the program.

/* This program contains an error */

class Error
 {
 public static void main (String args[ ] )
  {
   System.out.println (“Hello Java!”)    // missing ;
   }
 }
The java compiles does a nice job of telling as where the errors are in the program.

Error 1. Java: 7:1; 1 expected.

System.out.println (“Hello.Java !”)

^error

We can now go to appropriate line, correct the error is recompile the program.
Most of the compile time errors are due to mistakes. Typographical errors are hard to find we many bore to check the code word by odd or even character by character. The most common problems are:

  ·         Missing semicolons
  ·         Miss spelling of identifiers and keywords.
  ·         Missing ( (or) mismatch of) brackets in classes is methods
  ·         Missing double quotes in strings.
  ·         Use of undeclared variables.
  ·         In compatible type in assignments / in initialization.
  ·         Bad reference to objects.
  ·         Use of = in place of = =operation.

Run- time errors:

 Sometimes a program may compile successfully creating the class file but may not run program property such program may produce wrong results due to wrong logic or may terminate due to errors. Such as stack ones flow. Most common runtime errors are.

  ·         Dividing an integer by zero
  ·         Accessing an element that is out of the bounds of an array
  ·         Trying to store a value in to an array of an incompatible class or type.
  ·         Trying to illegally change the state of a thread.
  ·         Attempting to use a negative size for an array.
  ·         Accessing a character that is out of bounds of a string.

When such error are encountered, Java typically generates an error message and aborts the program.

Ex:  class error 2
        {
           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);
}
}
  The above program is systematically correct and therefore does not cause any problem during compilation. However while executing. It displays the following message and stops without executing further statements.

java .lang. ArithmeticException ://by zero

When Java runtime tries to execute a division by zero, it generates on error condition which causes the program to stop after displaying an appropriate message.


                                                                                                           
















Related

Java 329512787430550418

Post a Comment

emo-but-icon

item