Skip to main content

Introduction to Java Programming

  What is Java? Java is a programming language and a platform. Java is a high-level, robust, object-oriented and secure programming language. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. Firstly, it was called  Greentalk  and the file extension was  .gt . After that, it was called  Oak . Initially Java was designed for small, embedded systems in electronic appliances like set-top boxes. Platform:  Any hardware or software environment in which a program runs, is known as a platform. Since Java has a runtime environment (JRE) and API(Application Programming Interface), it is called a platform. The principles for creating Java programming were "Simple, Robust, Portable, Platform-independent, Secured, High performance, Multithreaded, Architecture neutral, Object-oriented, Interpreted, and Dynamic". Currently, Java is used in internet programming, mobile devices, games, e-business so

25. Errors in Java

 

Errors in Java

  • An error defines a reasonable issue that is topping the proper execution of the program.
  • Programming errors often remains undetected until the program is compiled or executed.
  • Errors should be removed from compiling and executing.
  • In Java, an Error is a subclass of Throwable class.

Types of Errors

Generally, there are 3 types of errors which are as:

Compile-Time or Syntax Error

  • These are those errors which prevent the code from running because of incorrect syntax.
  • These errors are detected by the compiler and an error message is displayed onto the screen while displaying.
class CompileTimeError {
    public static void main(String[] arr) {
        int a = 4 // No semicolon

        int b;
        b = 2.5;// Declared variable type and initialize variable type are different

        c = 8; // Variable data-type not declared
       
        System.out.println(a + d); // Variable d not declared
    // Missing parenthesis
}

Output :

CompileTimeError.java:3: error: ';' expected
int a = 4 // No semicolon
            ^
1 error

Run-Time Errors or Exceptions

  • These are detected during the execution of the program.
  • During compilation, the compiler has no technique to detect these kinds of errors.
  • Sometimes, these are discovered when the user enters an invalid data or data which is not relevant.
  • It occurs when user asks the computer to do something that the computer is unable to reliably do.
import java.util.Scanner;

class RunTimeError {
    public static void main(String[] arr) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number for dividing 1625: ");
        int num = input.nextInt();
        System.out.println("Integer part of 1625 dividing by " + num + " is: " + 1625 / num);
        input.close();
        System.out.println("Success...");
    }
}

Output :

Enter a number for dividing 1625: 65
Integer part of 1625 dividing by 65 is: 25
Success...

Exception in thread "main" java.lang.ArithmeticException: / by zero
          at RunTimeError.main(RunTimeError.java:20)

Logical or Semantic Error

  • When our program compiles and executes, but executes some different output.
  • These errors are neither detected by compiler nor by the JVM.
  • These errors are caused due to an incorrect idea or concept used by the programmer while coding.
class LogicalError {
    public static void main(String[] arr) {
        int a = 1234, rev = 0;
        System.out.println("Reversing a number...");
        while (a != 0) {
            rev = rev * 10 + a / 10; // rev = rev*10 + a%10
            a = a / 10;
        }
        System.out.println("The reverse of 1234 is " + rev);
    }
}

Output :

Reversing a number...
The reverse of 1234 is 124210

Previous

Next

Comments