You are on page 1of 9

Java exceptions handling

mechanism

Java exceptions handling mechanism - Mwango


Exception
• An exception (short for "exceptional event") is an error or
unexpected event that happens while a program is
running.
• When an exception occurs, it interrupts the flow of the
program. If the program can handle and process the
exception, it may continue running. If an exception is not
handled, the program may be forced to quit.
An exception can occur for many different reasons.
Following are some scenarios where an exception
occurs.
•A user has entered an invalid data.
•A file that needs to be opened cannot be found.
•A network connection has been lost in the middle of
communications or the JVM has run out of memory.
Java exceptions handling mechanism - Mwango
Exception
 Other languages only use exceptions to catch
fundamental runtime errors, such as failure
allocating memory or system-level errors. For
example, a C++ program may throw
the bad_alloc exception when memory cannot be
allocated and the system error exception when
the operating system produces an error.

There are 5 keywords used in java exception handling.


•try
•catch
•finally
•throw
•throws
Java exceptions handling mechanism - Mwango
Exception Handling
A well-written computer program checks for exceptions and
handles them appropriately. This means the developer must
check for likely exceptions and write code to process them.
If a program handles exceptions well, unexpected errors can
be detected and managed without crashing the program.
Exceptions are "thrown" when they occur and are "caught"
by some other code in the program. They can be thrown
explicitly using the throw statement or implicitly within
a try clause.

Java exceptions handling mechanism - Mwango


Exception Handling
Below is an example of "try / catch" syntax in Java.
The following code attempts to divide by zero, but throws
an ArithmeticException exception and returns 0 as the result.

1. int a = 11;
2. int b = 0;
3. int result = 0;
4. try {
5. int c = a / b;
6. result = c;
7. } catch(ArithmeticException ex) {
8. result = 0;
9. }
10. return result;

An exception is thrown on line 5 (when 11 is divided by 0),


so the remainder of the try statement (line 6) is not
executed. Instead, the exception is caught on line 7 and a
result of 0 is returned. Java exceptions handling mechanism - Mwango
Exception Hierarchy

Java exceptions handling mechanism - Mwango


 Example 1: NullPointerException
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

 Example 2. ArrayIndexOutOfBoundsException,
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException

Example 3
statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;
Suppose there is 10 statements in your program and there occurs an
exception at statement 5, rest of the code will not be executed i.e. statement
6 to 10 will not run. If we perform exception handling, rest of the statement
will be executed. That is why we use exception handling in java.

Java exceptions handling mechanism - Mwango


Summary
 An exception (or exceptional event) is a problem that arises
during the execution of a program. When an Exception occurs the
normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not
recommended, therefore, these exceptions are to be handled.

•Exception Handling is a mechanism to handle runtime


errors such as ClassNotFound, IO, SQL, Remote etc

•The core advantage of exception handling is to maintain


the normal flow of the application. Exception normally
disrupts the normal flow of the application that is why we
use exception handling

Java exceptions handling mechanism - Mwango

You might also like