Hello Learners, This is a post about What is Exception in Java, and how can be handled this? As we know, Exceptions are existed on everywhere, not only for a programming language, Exception is a word that is used in performing an abnormal activity. Exception is a type of mechanism that can be occurred at any time, any place. Some exceptions are known for users and some are unknown(it can be dangerous if it haven’t controlled).
What do you mean by Exception in Java
Table of Contents
So Generally, In Java Programming language, An Exception is an abnormal condition that arises in the sequence of code at the run time. In other words, an exception is also called run time error. In Object-Oriented World, this mechanism has been taken a great place according to its advantages. In any other programming language, if it doesn’t support, run-time errors can make the biggest deal or cumbersome deal for programmers or developers. Exceptions can be managed through the end-user and can be controlled through the java run time system. Java Run-time system is the automated process to throw the exception if it occurs. Java Exceptions is defined as in the form of java objects in java programming language to manipulate easily. we can handle the exception manually using 5 java pre-defined keywords such as try, catch, throw, throws, finally and this process is called Exception Handling.
Fundamental of Exception Handling in Java
A java exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises, an object representing the exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself, or pass it on. Either way, at some point, the exception is caught and processed and handled by manually using a combination of five keywords that are defined in the above paragraph. Ok Hakuna Matata (don’t worry), let’s start to explain about these keywords briefly.
We can use it to handle the exception using these keywords that are defined in the below section.
Try – First keyword to obtain Exceptions
This is a first keyword that can be used to obtain or get the exception where it is likely to happen and use it to throw the exception.
Catch – Handle the exception manually and efficiently
It is a type of block to catch the exception that is thrown by try block can be managed or handle the exception manually. It is an essential block to the user to maintain the exception manually.
throw – throw exception
It is a keyword to throw the exception manually.
throws – must be specified in this clause
Any exception that is thrown out of a method must be specified as such as by throws clause.
finally -last but important
This is a block in which all statements will run always after try block if an exception happens or not.
Exception Handling Example
Without Exception handling – Example
File Name – ExceptHand.java
class ExceptHandling { static int div; static int a=20; static int b=0; public static void main(String except[]) { ExceptHandling E=new ExceptHandling(); do_devide(a,b); } static void do_devide(int a,int b) { div=a/b; System.out.println("Result is --"+div); } }
It will provide following result.
With Exception Handling – Example
class ExceptHandling
{
static int div;
static int a=20;
static int b=0;
public static void main(String except[])
{
ExceptHandling E=new ExceptHandling();
do_devide(a,b);
}
static void do_devide(int a,int b)
{
try{
div=a/b;
}
catch(ArithmeticException AE){
System.out.println(“Your denominator is zero – result is infinite.”); }
}
}
This will provide following result.
Pingback: How to Disable Third-Party Cookies in Chrome >> Bittu Tech