What do you do when you see a speed breaker in front of you?
you apply breaks, right?
The speed breaker breaks down your normal speed.
This is exactly what exception does to our program.
An exception has the same meaning in programming as in real life. we normally use the word this is an exceptional case.
What is your action when you see speed breaker in front of you?
You apply breaks, right?
The speed breaker breaks down your normal speed.
This is exactly what exception does to our program.
What Is An Exception?
Exception is an event that cause to disrupts the regular flow of the program.
Exception handling enables us to handle runtime errors.
Thus, it maintains the normal flow of the program.
Exceptions can occur due to user’s error, programmer’s error or due to the failure of any physical resource.
There are two types of exceptions:
- Checked exception
- Unchecked exception
A checked exception is an exception that occurs at compile-time, these are also known as compile-time exceptions.
The programmer needs to handle these exceptions.
An unchecked exception is an exception that occurs at the time of execution, these are also known as runtime exceptions.
Logical errors or programming bugs come under unchecked exceptions.
Errors are not exceptions.
In programming, errors are something which is out of user’s control or programmer’s control and cannot be handled like exceptions.
Try, catch and finally
Try, catch and finally are used to handle exceptions in Java.
The try block is used to cover the block of code which might throw an exception.
The try block is always followed by catch or finally block.
The catch block is used to handle the exception.
It is always used after a try block.
There can be multiple catch blocks followed by a try block.
try{
//code
}
catch(exception_name e){
//catch block
}
This is how a try catch block looks like.
exception_name e points to the exception thrown if any.
In Java, finally block always follows a try or catch block.
This is the block of code which is executed irrespective of whether the execution is handled or not.
try{
//try block
}
finally{
//finally block
}
This is how a try finally block looks like.
Let’s write a try, catch block in order to catch the exception thrown when a number is divided by 0.
class Main {
public static void main(String[] args) {
try{
int a = 10/0;
}
catch(Exception e){
System.out.println(e);
}
}
}
The output of the above written code will be
java.lang.ArithmeticException: / by zero
The output of the snippet says java.lang.ArithmeticException which is the exception thrown when an arithmetic exception is encountered.
And / by zero is the exception which has occurred.
Let’s add a finally block at the end of the code.
class Main {
public static void main(String[] args) {
try{
int a = 10/0;
}
catch(Exception e){
System.out.println(e);
}
finally{
System.out.println(“Division by zero.”);
}
}
}
The output of the above written code will be
java.lang.ArithmeticException: / by zero Division by zero.
The finally block is executed and Division by zero is displayed below the caught exception.
This is how the try, catch and finally blocks work.
Let’s see why throw is used.
Throw
The throw keyword is used to show/throw an detailed exception(sometimes customized).
The syntax of the java throw exception is
throw new exception;
Let’s see how throw works.
class Main {
static void div(int num){
if(num == 0)
throw new ArithmeticException(“Division by 0.”);
else
System.out.println(20/num);
}
public static void main(String[] args) {
div(0);
}
}
The output of the above written code will be
Exception in thread “main” java.lang.ArithmeticException: Division by 0.
Thus, using throw we have thrown a custom exception which says Division by 0.
We’re done with throwing, catching and mending things finally.
To summarize
- Exception is an event that happens during programming execution and disrupts the Normal flow.
- Exception handling maintains the normal flow of the program.
- The try block is used to cover the block of code which might throw an exception.
- The catch block is used for catching and handling exceptions.
- The finally block is always executed irrespective of whether an exception is thrown or not.
- The throw keyword is used for throwing an detailed exception.