0% found this document useful (0 votes)
294 views

Errors in Java - 9th - ICSE

The document discusses three types of errors in Java: syntax errors, logical errors, and runtime errors. Syntax errors are detected during compilation and prevent code from running due to incorrect syntax. Logical errors cause programs to produce wrong or unexpected output despite compiling and running. Runtime errors occur during execution when the program asks the computer to do something it cannot, such as divide by zero.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
294 views

Errors in Java - 9th - ICSE

The document discusses three types of errors in Java: syntax errors, logical errors, and runtime errors. Syntax errors are detected during compilation and prevent code from running due to incorrect syntax. Logical errors cause programs to produce wrong or unexpected output despite compiling and running. Runtime errors occur during execution when the program asks the computer to do something it cannot, such as divide by zero.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Errors in java

SYNTAX ERROR, LOGICAL ERROR, RUNTIME ERROR


Syntax Error / Compile Time Error
Compile Time Errors are those errors which prevent the code from running because of an incorrect
syntax such as a missing semicolon at the end of a statement or a missing bracket, class not found, etc.
These errors are detected by the java compiler and an error message is displayed on the screen while
compiling.
Compile Time Errors are sometimes also referred to as Syntax errors.
These kind of errors are easy to spot and rectify because the java compiler finds them for you. The
compiler will tell you which piece of code in the program got in trouble and its best guess as to what
you did wrong.
Usually, the compiler indicates the exact line where the error is, or sometimes the line just before it,
however, if the problem is with incorrectly nested braces, the actual error may be at the beginning of
the block.
In effect, syntax errors represent grammatical errors in the use of the programming language.
Syntax Error/Compile Time Error
class PrintingSentence {
public static void main(String args[])
{
String s = "GeeksforGeeks";

// Missing ';' at the end


System.out.println("Welcome to " + s)
}
}

Compilation Error in java code:


prog.java:8: error: ';' expected
System.out.println("Welcome to " + s) ^ 1 error
Logical Error / Semantic Error
A logic error is when your program compiles and executes, but does the wrong thing or returns
an incorrect result or no output when it should be returning an output.
These errors are detected neither by the compiler nor by JVM. The Java system has no idea what
your program is supposed to do, so it provides no additional information to help you find the
error.
Logical errors are also called Semantic Errors. These errors are caused due to an incorrect idea or
concept used by a programmer while coding.
Syntax errors are grammatical errors whereas, logical errors are errors arising out of an incorrect
meaning.
For example, if a programmer accidentally adds two variables when he or she meant to divide
them, the program will give no error and will execute successfully but with an incorrect result.
Logical Error / Semantic Error
public class LErrorDemo {
public static void main(String[] args)
{
int num = 789;
int reversednum = 0;
int remainder;

while (num != 0) {

// to obtain modulus % sign should


// have been used instead of /
remainder = num / 10;
reversednum
= reversednum * 10
+ remainder;
num /= 10;
}
System.out.println("Reversed number is“ + reversednum);
}}

Reversed number is 7870


Runtime Error
Run Time errors occur or we can say, are detected during the execution of the program.
Sometimes these are discovered when the user enters an invalid data or data which is not
relevant.
Runtime errors occur when a program does not contain any syntax errors but asks the computer
to do something that the computer is unable to reliably do.
For example: if the user inputs a data of string format when the computer is expecting an
integer, there will be a runtime error.
Runtime Error
// Java program to demonstrate Runtime Error
class DivByZero {
public static void main(String args[])
{
int var1 = 15;
int var2 = 5;
int var3 = 0;
int ans1 = var1 / var2;

// This statement causes a runtime error,


// as 15 is getting divided by 0 here
int ans2 = var1 / var3;
System.out.println("Division of va1"
+ " by var2 is: "+ ans1);
System.out.println( "Division of va1"
+ " by var3 is: " + ans2);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero at DivByZero.main(File.java:14)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy