2. Errors are the wrong that can make a program go wrong. It is
therefore important to detect and manage properly all the possible
error condition in the program so that the program will not
terminate or crash during execution. There are 2 types of Errors.
Namely,
Compile-time errors
Run-time errors
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
Run-time errors :- Run time errors are type of errors which are
occurred by dividing an integer by zero, trying to cast an instance
of a class to one of its sub class.
3. Error occurred in execution time.
Abnormal termination of program.
Wrong execution result.
Provide an exception handling mechanism in
language system.
Improve the reliability of application program.
Allow simple program code for exception check
and handling into source.
4. Exception is an object thrown when a runtime
error occurs in the program.
Class Exception
Class Runtime Exception
Class Error
Checked and un checked Exception
5. Class Exception: The class Exception represents exceptions
that a program would want to be made aware of during
exception.
Class Runtime Exception : Runtime exceptions is a subclass
of the exception class. The runtime exceptions are usually
caused by program bugs i.e., they are faults in the program
design.
Class Error: Subclasses of the java.lang.Error class define
exceptions that indicate class linkage(linkage Error), thread
(thread Death), and virtual machine(Virtual Machine Error)
related problems.
6. Checked and unchecked Exception: Except for
Runtime Exception, Error, and their subclasses, all
exceptions are called checked exceptions. The
method must either catch the exception and take the
appropriate action, or pass the exception on to its
caller.
8. public static void main(String str[])
{
int y=0;
int x=1;
//a division by 0 occurs here.
int z=x/y;
System.out.println(“ After division”);
}
9. The first step in constructing an exception handler is
to enclose the code that might throw an exception
within a try block.
General syntax:
10. Exception handlers with a try block are associated
with one or more catch blocks directly after the try
block. No code can be between the end of the try
block and the beginning of the first catch block.
Syntax: try
{
}
catch(ExceptionType name)
{
}
11. The Finally block always executes when the try block exits.
This ensures that the finally block is executed even if an
unexpected exception occurs. It creates a block of code that
will be executed after a try/catch block has completed and
before the code following the try/catch block. Finally block will
execute whether an exception is thrown or not. If an exception
is thrown, the finally block will execute even if no catch
statement matches the exception.
Syntax: finally
{
// statements in finally will get executed
exception occurs or not.
}
12. try block
No Exception Exception Occurs
finally catch
finally
13. Unchecked exceptions: exceptions derived from runtime
exception class or automatically available, they need not to
be included in any method “throw’s” list. These are called as
unchecked exception because the compiler doesn't click to
see, if a method handles or throws an exception.
Checked Exception: exception defined by java.lan that must
be included in a method “throw’s” list ,if that method can
generate one of these exception and doesn't handle itself are
called Checked exception.
Eg: classNotFoundException
InterruptedException