Você está na página 1de 9

Introduction

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.

Error in Java are of two type-

1. compile-time Error : Compile-time Error are those error which occurs


when you don't follow the Syntax of the code or do wrong in writing a code
of the Programming language. The Compiler detect the Syntax error in the
code of the Programming language during its execution. For example in
normal java coding, you need your every statement to be terminated with
semicolon(;). If you don't follow the rule, this will give you Compile-time
error.

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.

Why Exceptions Occur

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-

1. Search a file which does not exist.


2. Dividing a number by zero.
3. Allocating Memory error.
4. Problem in accessing Network Connectivity.

What is Exception Class

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()

The Syntax represent a constructor of throwable class with no arguments.

In the same way we can declare a constructor of Throwable class with a


userdefined Parameter.

Throwable(String Parameter)

Parameter can be message that may be userdefined included in Throwable class.

Exceptional Class

1. ClassNotFoundException- Exception arises when a class is being


reffered,but no such definition of class name is being found.
2. RuntimeException- handles the Exception arises during the runtime
execution of the program.
3. IllegalAccessException-This Exception is thrown when method is not
found.
Exceptional Hierarchy
Built in Exception

The built-in exception in java are of following types classified on the basis of
exception handled by the java compiler.

1. Checked Exception : These exception are the objects of the Exception


class and its subclasses. It don't include the Run-time Exception. These
exception occurs due to invalid user input, problem in the network
connectivity, database connection. This also arises when we forget to import
the packages. For Example – java.sql.sqlexception, java.io.ioexception. The
Exception problem can be overcome by implementing Try-Catch block which
u see in Example in exception.

2. Unchecked Exception : These exception are the Run-time errors that


arises due to incorrect arguments passed to the public method written by
the programmer. The Compiler never checkes the Unchecked exception
during the program compilation. For Example – Dividing any number by
zero is an unchecked exception.
When an error occurs in a method, Java creates an object of the Exception
method. On making an object of exception method, java send it to the program
by throwing the object of exception method. The Exception Object states the
information of type of error and status of program during the exception Occurred.

Following are the Keyword used in implementing Exception-Handling-

1. Try

2. Catch

3. Throw

4. Throws

5. Finally

How to implement Try-Catch Block :

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.

Let Us Understand With Example -


class Exception Unhandled

Public static void main(String args[])

{
int num1=0;

int num2=8;

int num3=num2/num1;

System.out.println("The num3 = " +num3);

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.

Output in Command Prompt -

C:\Documents and Settings\Administrator>cd\

C:\>cd new folder\

C:\New Folder>javac ExceptionUnhandled.java

C:\New Folder>java ExceptionUnhandled


Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionUnhandled.main(ExceptionUnhandled.java:7)

See How we going to implement Try-Catch Block in the above coding -

Inorder to handle exception, you need to implement the Try-Catch. If an


exception arises within a try block, the appropriate exception handler associated
with try block handles the exception. The Catch block catches the object of the
exception class as a parameter. Once the exception is caught in catch-block, the
expression within the corresponding block is excuted.

class ArithmeticException

public static void main(String args[])

int num1 = 0;

int num2=10;

int num3=0;

try

num3=num2/num1;

System.out.println("The result =" +num3);

catch(ArithmeticException e)

System.out.println("Division by Zero is done");

Output in Command Prompt -


C:\java>javac ArithmeticExcep.java
C:\java>java ArithmeticExcep
Division by Zero is done
Using the Throw -

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.

The Syntax used to declare the throw statement :

throw Throwable objt

The above Syntax, Throwableobjt is an object of the class Throwable.


Throwableobjct object is created using the new operator. The compiler gives you
an error if the throwableobjt does not belong to a valid class of Exception.

The following Example help you to understand the throw -

class ThrowState

static void throwdemostration()

try

throw new IllegalStateException();

catch (NullPointerException objetB)

{
System.out.println("Not caught by catch block inside throwdemostration().");

public static void main(String args[])

try

throwdemostration();

catch(IllegalStateException objetC)

System.out.println("Exception Caught in:"+ objetC);

In the above given code, the new operator create an object of


IllegalStateException. In the throwdemostration() method, an exception
IllegalStateException is thrown.

Output of the code in Command Prompt -


C:\java>javac ThrowState.java
C:\java>java ThrowState
Exception Caught in:java.lang.IllegalStateException
C:\java>

Você também pode gostar