1. Errors
Compile time errors: These are syntactical errors found in
the code, due to which a program fails to compile. E.g.
missing semicolon at the end of statement, a statement
without proper syntax etc.
Run time errors: These errors represent inefficiency of the
computer system to execute a particular statement. E.g.
insufficient memory to store something or inability of the
microprocessor to execute some statement come under
run-time error.
public static void main( )
2. Errors…
Logical errors: These errors depict flaws in the logic of the
program. The programmer might be using a wrong
formula or the design of the program itself is wrong.
Logical errors are not detected either by java compiler or
JVM.
Exceptions
Checked: The exceptions that are checked at compilation
time are called ‘Checked Exception’.
Unchecked: The Exceptions that are checked by JVM are
called ‘Unchecked Exceptions’.
3. Exceptions…
Unchecked exceptions are considered as unrecoverable
and the programmer cannot do any thing when they
occur. Programmer can write a java program with
unchecked exceptions, he can see their effect only when
he runs the program.
In case of checked exception, a programmer should either
handle them or throw them without handling them. He
cannot simply ignore the checked exception.
public static void main(String args[]) throws IOException
4. Exceptions…
class Test
{
public static void main(String args[])
{
int n = args.length;
int a = 45 / n;
System.out.println(“The value of a is :”+a);
}
}
5. Exceptions Handling
When there is an exception, the user data may be
corrupted. To handle the exception, programmer should
perform 3 steps:
1. try{
Statements;
}
2. catch(Exceptionclass ref) {
statements;
}
3. finally{
statements;
}
6. Exception Handling…
class Test
{
public static void main(String args[])
{
try {
int n = args.length;
int a = 45 / n;
System.out.println(“The value of a is :”+a); }
catch(ArithmeticException ae) {
System.out.println(ae);
System.out.println(“Arguments are required”); }
finally {
System.out.println(“End of program”); }
}
}
7. Handling Multiple Exception
class Test {
public static void main(String args[]) {
try {
int n = args.length;
int a = 45 / n;
System.out.println(“The value of a is :”+a);
int b[] = {10,20,30};
b[5] = 100; }
catch(ArithmeticException ae) {
System.out.println(ae);
System.out.println(“Arguments are required”); }
catch(ArrayIndexOutOfBoundsException aie) {
aie.printStackTrace();
System.out.println(“Array index out of range”); }
finally {
System.out.println(“End of program”); }
} }