Sie sind auf Seite 1von 8

5.

Handling Errors and Exceptions

Dr.Narayana Swamy Ramaiah


Assoc.Prof, Dept of Electrical and
Computer Engineering,
Arba Minch University
What is an Exception
• Exception is an event that occurs during execution of a program that disrupts
normal flow of instruction.

• Let's take a scenario:


instruction 1;
instruction 2;
instruction 3;
instruction 4;
instruction 5; //exception occurs
instruction 6;
instruction 7;
instruction 8;
instruction 9;
instruction 10;
• Suppose there is 10 instructions in your program and there occurs an exception at
instruction 5, rest of the code will not be executed i.e. instruction 6 to 10 will not
run.
• When an error occurs within a method, the method creates an exception object and hands it off to the
runtime system, this is called throwing an exception.

• After a method throws an exception, the runtime system attempts to find something to handle it. The set
of possible "somethings" to handle the exception is the ordered list of methods that had been called to
get to the method where the error occurred. The list of methods is known as the call stack (see the next
figure).

The call stack.

• The runtime system searches the call stack for a method that contains a block of code that can handle the
exception. This block of code is called an exception handler. The search begins with the method in which
the error occurred and proceeds through the call stack in the reverse order in which the methods were
called. When an appropriate handler is found, the runtime system passes the exception to the handler. An
exception handler is considered appropriate if the type of the exception object thrown matches the type
that can be handled by the handler.

• The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches
all the methods on the call stack without finding an appropriate exception handler, as shown in the next
figure, the runtime system (and, consequently, the program) terminates. Searching the call stack for the
exception handler.
Exception Types
• There are two types of exceptions

– Checked exceptions (checked at compile time)


• DivideByZero exception
• ClassNotFoundException
• IllegalAccessException
• NoSuchFieldException
• EOFException etc.

– Unchecked exceptions (checked at runtime)


• ArithmeticException
• ArrayIndexOutOfBoundsException
• NullPointerException
• NegativeArraySizeException etc.

• Error (irrecoverable-Thread Death)


• OutOfMemoryError,
• VirtualMachineError,
• AssertionError etc.
• All exception classes are subtypes of the
java.lang. Exception class.
• The exception class is a subclass of the
Throwable class. Other than the exception
class there is another subclass called Error
which is derived from the Throwable class.
• The Exception class has two main subclasses:
IOException class and RuntimeException Class.
• There are given some scenarios where unchecked exceptions can occur. They
are as follows:

• Scenario where ArithmeticException occurs

If we divide any number by zero, there occurs an ArithmeticException.

int a=50/0; //ArithmeticException

• Scenario where NullPointerException occurs

If we have null value in any variable, performing any operation by the


variable occurs an NullPointerException.

String s=null;
System.out.println(s.length()); //NullPointerException
• Scenario where NumberFormatException occurs

The wrong formatting of any value, may occur NumberFormatException.


Suppose I have a string variable that have characters, converting this
variable into digit will occur NumberFormatException.

String s="abc";
int i=Integer.parseInt(s); //NumberFormatException

• Scenario where ArrayIndexOutOfBoundsException occurs

If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:

int a[]=new int[5];


a[10]=50; //ArrayIndexOutOfBoundsException

Das könnte Ihnen auch gefallen