Exception handling.

In this article,Exception handling is explained in detail with examples.
The exception is the error which may occur when a java program is getting executed. Technically it is known as "Runtime error".
Exception Handling-javaform
Definition
"Exception handling" is a mechanism in which runtime errors are detected accepted and provided with the solution.
  • In doing so, lifetime or durability of the product increases.
  • In real time,product manufacturing companies provide service centers so that any product being used by the customer and faces any problem, such problem is accepted and provided with the service center in the form of warranty or guarantee. (From topic Exception handling)
Ex
Example of ExceptionHandling-Javaform
Exception Object
Since java is pure object oriented programming language when the exception occurs during execution time,JVM dynamically or automatically creates an object in memory, such object is known as exception-object. (From topic Exception handling)
This exception object has three characteristic
  • Exception type
  • Exception message
  • Exception state
Hierarchy of exception
Java provides some predefined exception classes,using which,we can perform exception handling mechanism simple and easy.This hierarchy is as follows
All these exception classes are available in java.lang package.


Note
In general Exceptions are two types
  • Checked Exceptions.
  • Unchecked Exceptions.
Checked exceptions are known exceptions,during compilation time only we provide necessary solution.ie, java compiler tells the user that exception may occur at a particular line in your program this is done during compilation time.
Unchecked exceptions are unknown exceptions, during compilation time,these exceptions are not shown by java compiler
i.e,User or developer provides proper necessary solutions based on exception occur during execution time. (From topic Exception handling)
Steps for exception handling
  • Monitor or detect the exception in your program logic.
  • Warn or alert message the exception has occurred.
  • Accept the exception occurred.
  • Handle or provide a solution for the exception.
Note
After exception occurs and solution is not provided then it leads to "Abnormal termination of lava program/application"
i.e, Breakdown of software.
Exception handling in java program is done using the following
1.try block
It is a block of code preceded by the keyword try.
Ex
try
{
----
---
}
It is used in the method of our program to detect or monitor the exception.
2.throw keyword
It is used in try block to warn or alert that exception occurred in your program throw keyword is used with a condition.
Ex
try
{
--
if(condition)
throw(Exception object)
---
}
Note
For all predefined exception classes,JVM  alter or detects exceptions automatically.
Hence we use throw keyword especially for user-defined exceptions.
3.Catch-block
It is a block of code used to accept the exception which is thrown from try block.It is used immediately after try block. (From topic Exception handling)
Ex
try
{
---
---
}
catch(Exception object)
{
---
----
--
(handle or provide the solution )
----
}
Here catch-block takes exception object as parameter the exception which is thrown from try block should match with catch-block parameter otherwise it leads to abnormal termination of java program i.e, break down.
*In catch-block,we provide solution i.e, Exception handling mechanism is done successfully.
Ex
public class Exception1
{
public static void main(String args[])
{
int i=1,j=0,k;
k=i/j;
System.out.println("Hello world");
}
}
Output
printing exception-javaform

Note
Abnormal termination leads to non-execution of remaining statements of the program.
Ex
//program related to Exception handling.
public class Exception11
{
public static void main(String args[])
{
int i=1,j=0,k;
try
{
k=i/j;
System.out.println("Hello, How are you?");
}
catch(ArithmeticException ae)
{
System.out.println("Division by zero.Illegal operation");
System.out.println("Hello world");
}
}
}
Output
Exception11-javaform

Note
An exception occurred in try-block,remaining statement and in try-block are skipped and control comes directly to immediate catch-block.
Printing exception object
In general, when object reference variable is printed we get class-name @hexadecimal-number but when exception object is printed, we get
  • Exception type
  • Exception message
This happens because internally when the exception object is printed.JVM calls to string() method of the object class, this is responsible for printing exception type and message. (From topic Exception handling)
Ex
//program related Exception handling.
public class Exception3
{
public static void main(String args[])
{
int i=1,j=0,k;
try
{
k=i/j;
System.out.println("hello,How are you");
}
catch(ArithmeticException ae)
{
System.out.println("Exception is ::"+ae); //printing exception-object-ref-var
}
System.out.println("Hello world");
}
}
Output
Exception3-Javaform

Mismatching catch block
After exception object is throw out from try-block and there is no proper matching catch-block or there is mismatching catch block then again it leads to abnormal-termination of java program
Ex
public class Exception4
{
public static void main(String args[])
{
int i=1,j=0,k;
try
{
k=i/j;
}
catch(ClassCastException e)
{
System.out.println("Division by zero,illegal

operation");
System.out.println("Hello world");
}
}
}
Output
MismatchingCatchBlock.-output-javaform

Multiple catch blocks
For a single try block, we can have multiple catch blocks based on the type of exception raised in try block, the corresponding catch block is executed.
Ex
public class Exception41
{
public static void main(String args[])
{
int i=1,j=0,k;
try
{
k=i/j;
}
catch(ClassCastException e)
{
System.out.println("Division by zero,Illegal operation");
}
catch(ArithmeticException ae)
{
System.out.println("Division by zero,illegal operation");
}
System.out.println("Hello world");
}
}
Output
Multiplecatchblocks-output-Javaform

Ex
public class Exception2
{
static String ss="Hello world";
//static String ss=null;
public static void main(String args[])
{
int i=0,j=5,k;
try
{
k=i/j;
System.out.println("K=="+k);
System.out.println(ss.length());
}
catch(ArithmeticException e)
{
System.out.println("Division by zero,illegal operation");
}
catch (NullPointerException e)
{
System.out.println("operation on null is not possible");
}
System.out.println("Hello world");
}
}
Output
Missmatchcatch-blocks2-javaform

Note
Any operation on the null value in a program leads to null pointer exception.
Catching related exceptions in hierarchy
Note
Hence if exception raised in try block is not known then we can catch such exception-type using top-level.
Base-class type
Ex:
//program related to Exception handling-Exception class.
public class Exception6
{
public  static void main(String args[])
{
int i=1,j=0,k;
try
{
k=i/j;
}
catch(RuntimeException e)
{
if(e instanceof ArithmeticException)
{
System.out.println("Division by zero is illegal");
}
System.out.println("Hello world");
}
}
}
Output
Exceptionclass.-output-javaform

Continue to the next topic Synchronization in Java.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can support to establish your own startup through Digital Marketing

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required