Escolar Documentos
Profissional Documentos
Cultura Documentos
Exception is a run-time error which arises during the execution of java program.
The term exception in java stands for an exceptional event. It can be defined as
abnormal event that arises during the execution and normal flow of program. The
exceptional event can also be error in the program.
2. Run-time Error : Run-time Error are the error which arises during the
execution of a program. For example. The program runs out of the memory,
result in Run-time error.
An Exception is Run-time error that arises during the normal execution of Java
program. In case of Run-time error, if you divide a number by zero or unable to
open a file which does not exist, an exception is raised. In java exception are
handled by Run-time System or user-defined code. A run-time error throws an
exception.
Error handling plays a important role while developing an application. Following
are the situation in which run-time error arises-
Object Class is the base class of the exception hierarchy. Object class is the super
class of Throwable class. Throwable class is the super class of all the exceptional
class and error class. In java you can throw exception Object which are derived
from throwable class. The following Syntax for declaring a throwable class .
Throwable()
Throwable(String Parameter)
Exceptional Class
The built-in exception in java are of following types classified on the basis of
exception handled by the java compiler.
1. Try
2. Catch
3. Throw
4. Throws
5. Finally
The Try Block contain a set of statements that raise an exception-event within the
scope of method. If the exception arises in Try-Block the appropriate handler with
the Try Block proceed the exception. As We Know, the Catch Block is used as
exceptional-handler. The Exception that arises in the try block is handled by the
Catch-Block. Each Try block preceded by one Catch block. The catch block
specifies the type of exception need to catch.
{
int num1=0;
int num2=8;
int num3=num2/num1;
The above code contain a class Exception Unhandled, Which on Compile gives us
Arithmetical exception with no exception being handled. In this code the Java
run-time a exception when a number is divided by zero. The output of the code
shows that the exception thrown is the object of the subclass of exceptional class.
class ArithmeticException
int num1 = 0;
int num2=10;
int num3=0;
try
num3=num2/num1;
catch(ArithmeticException e)
The throw an exception can be explicitly used by using the keyword throw
statement. For example, you have entered wrong username and password in
login form need to thrown an exception. The throw expression normally causes
the code to stop or terminate during normal flow of java code. The Throw proceed
the controls to nearest catch block. If there is no catch block, then program
terminates.
class ThrowState
try
{
System.out.println("Not caught by catch block inside throwdemostration().");
try
throwdemostration();
catch(IllegalStateException objetC)