Friday 24 December 2021

Types of errors in Java

An error may broadly classified into two categories: compile time errors and run time errors. An error is a mistake made by the user.

1. 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 compiler displays an error it will not create the dot class file (bytecode), it is therefore necessary that we fix all the errors before we can successfully compile and run the programe. The most common problems that gives compile time errors are 

  1. Missing a semicolon 
  2. Miss match of brackets in classes and methods
  3. Misspelling of  identifiers and keywords
  4. Missing a double codes in string 
  5. Use of undeclared variables
  6. Use of = symbol instead of = = 

Other errors we may encounter are related to directory paths such as " javac is not recognized as an internal or external command".

2. Runtime Errors 

Some times a program may compile successfully and creates the dot class file but may not run properly such program may produce wrong results due to wrong logic or may terminated due to errors such as StackOverFlow. Most common runtime errors are 

Dividing an integer number by zero

Accessing an element that is out of the boundaries of an array

Trying to store a value into an array of an incompatible class or type 

Passing a parameter that is not in a valid range or value for a method

Trying to illegally change the state of a thread.

When such errors are encountered, java typically generates an error message and terminates the program.

The follow program show how  exceptions occur 

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

Output


In the above program, it will successfully creates the dot class file but it will not produce any output. It will generate an error message indicating an exception occurs

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top