Exception Handling - Google Docs
Exception Handling - Google Docs
—------------------
Q)What is the difference between Error and Exception?
—------------------------------------------------------
Ans:
—---
Error is a problem , it will not allow us to execute applications.
1. Compilation Errors
2. Runtime Errors
Compilation Errors:
These errors are generated at compilation time.
There are three types of Compilation Errors.
1. Lexical Errors
2. Syntax Errors
3. Semantic Errors
Lexical Errors:
These errors are identified by the Lexical Analysis phase in Compilation.
EX: Mistakes in the tokens
EX: int i = 10; —---> Valid
nit i = 10; —---> Invalid, Lexical Error
Syntax Errors:
These errors are identified in the Syntax Analysis phase in Compilation.
EX: Mistakes in the syntaxes.
EX: int i = 10; —-> Valid
i 10 int; —---> Invalid, Syntax Error
EX: while(i < 10){
Sopln(i);
}
Status: Valid
(i < 10) while{
Sopln(i);
i = i + 1;
}
Status: Invalid, Syntax Error.
Semantic Error:
These errors are identified in the Semantic Analysis phase in compilation.
EX: Meaningless statements.
EX: Providing incompatible operands for the operators.
EX: int i = 10;
boolean b = true;
char c = i + b;
Note: Along with the above three types of errors, all the programming
languages are having their own errors as per the programming language rules
and regulations.
The errors which are identified at runtime and which are not having solutions
programmatically then these errors are “Runtime Error”.
Exception:
These are runtime errors identified at runtime and which are having solutions
programmatically.
EX: ArithmeticException
EX: NullPointerException
EX: FileNotFoundException
—----------------------------------------------------------------------------
Exception:
Exception is an unexpected event occurred at runtime of the application,
which may be provided by the users while entering dynamic input to the java
applications, which may be provided by the Databases when we perform database
operations from java applications in JDBC applications, which may be provided
by the network when we establish connection between client and Server in
Distributed applications,....... Causes abnormal termination to the
applications.
Predefined Exceptions:
—----------------------
These exceptions are defined by the Java programming languages , Java has
provided predefined classes for these exceptions in java predefined library.
Note: Really exceptions occur at runtime only, not at compilation time, but
Compiler can recognize some exceptions which are going to generate at runtime
then that exceptions are called “Checked Exceptions”.
All the remaining Exceptions are the examples for Checked Exceptions.
If we run the above program then JVM will provide the following exception
message.
The above Exception message is divided into the following three parts.
1. Exception Name : java.lang.ArithmeticException
2. Exception Description : / by zero
3. Exception Location : Main.java: 5
2. NullPointerException:
If we access any instance variable or instance method on a reference variable
containing null value then JVM will raise NullPointerException.
EX:
import java.util.Date;
If we run the above program then JVM will provide the following exception
details.
1. Exception Name: java.lang.NullPointerException
2. Exception Description : Cannot invoke "java.util.Date.toString()" because
"date" is null
3. Exception Location: Main.java: 6
3. ArrayIndexOutOfBoundsException:
In Java applications, when we access an element from an array on the basis of
the index value , when we insert an element in an array on the basis of the
index value, where the provided index value is in the outside range of array
indexes there JVM will raise ArrayIndexoutOfBoundsException.
EX:
import java.util.Date;
4. StringIndexOutOfBoundsException:
In Java applications , when we are performing String operations on the basis
of the index value , where the provided index value is in the outside range
of the String indexes there JVM will raise StringIndexOutOfBoundsException .
EX:
import java.util.Date;
If we run the above program then JVM will provide the following exception
details.
5. ClassNotFoundException:
In Java applications, if we want to load a particular class bytecode to the
memory then we will use the Class.forName() method.
JVM will search for Employee.class at the current location, at the predefined
library and at the locations referred by the classpath environment variable.
If we run the above program then JVM will provide the following exception
details.
6. InstantiationException:
In Java applications, by using Class.forName() method we are able to load a
particular class bytecode to the memory, after loading class bytecode if we
want to create an object for the loaded class we have to use the following
method java.lang.Class.
If we execute the above instruction, JVM will search for a 0-arg constructor
in the Employee class. If no 0-arg constructor is available in the Employee
class then JVM will raise InstantiationException .
EX:
class Employee{
Employee(int i){
System.out.println("Employee-Constructor");
}
}
public class Main {
public static void main(String[] args) throws
Exception {
Class cls = Class.forName("Employee");
Employee employee = (Employee) cls.newInstance();
}
}
If we run the above program then JVM will provide the following details
1. Exception Name: java.lang.InstantiationException
2. Exception Description : Employee
3. Exception Location : Main.java: 9
7. IllegalAccessException:
In Java applications, by using Class.forName() method we are able to load a
particular class bytecode to the memory, after loading class bytecode if we
want to create an object for the loaded class then we have to use the
following method from java.lang.Class.
If we execute the above instruction, JVM will search for a non private
constructor in the Employee class. If we have a private constructor in the
Employee class then JVM will raise IllegalAccessException .
EX:
class Employee{
private Employee(){
System.out.println("Employee-Constructor");
}
}
public class Main {
public static void main(String[] args) throws
Exception {
Class cls = Class.forName("Employee");
Employee employee = (Employee) cls.newInstance();
}
}
If we run the above code then JVM will provide the following exception
details.
Exception Name : java.lang.IllegalAccessException
Exception Description : class Main cannot access a member of class Employee
with modifiers "private"
Exception Location : Main.java: 9
8. ClassCastException:
In Java applications, we are able to keep subclass object reference value in
superclass reference variable, but we are unable to keep superclass object
reference value in subclass reference variable, if we keep superclass object
reference value in subclass reference variable then JVM will raise
ClassCastException.
EX:
class Employee{
}
class Manager extends Employee{
}
public class Main {
public static void main(String[] args) throws
Exception {
Employee employee = new Employee();
Manager manager = (Manager) employee;
}
}
If we run the above program then JVM will provide the following Exception
details.
1. Exception Name : java.lang.ClassCastException:
2. Exception Description : class Employee cannot be cast to class Manager
3. Exception Location : Main.java: 10
9. FileNotFoundException:
In Java applications, when we are trying to read data by using
FileInputStream or FileReader from a particular file and if the provided file
is not available then JVM will raise FileNotFoundException.
EX:
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws
Exception {
FileInputStream fileInputStream = new
FileInputStream("E:/abc/welcome.txt");
}
}
If we run the above code then JVM will provide the following exception
details.
‘throw’ Keyword:
—-----------------