Exceptions and Handling Exceptions, try and catch block.

Exceptions:

Exception is a run-time error. As, in the previous article we have learned about type of errors and when they may occur.

Since, some exceptions are checked at the time of compilation by Java Compiler.
And, some exceptions are checked at the time of run time by JVM(java virtual machine).

Now, let us have a look on Types of Exceptions:

1). Checked Exceptions: Exceptions that are checked by JAVA compiler at the time of compiling a JAVA program are called as Checked Exceptions.

2). Unchecked Exceptions: Exceptions that goes unchecked by JAVA compiler are called Unchecked Exceptions. These exceptions are checked by JVM.


public static void
main(String args[])throws IOException

Here, IO Exception is an example for Checked Exception. So, we threw it out of main() method without handling it. This is done by writing throws keyword after main() method.

ADVERTISEMENT



Exception Handling:

Exception handling means handling the checked and unchecked exceptions while we execute a program.
    Exception handling can be done in two steps. For, handling the exception try and catch block can be used.

Let us understand what comes under these blocks.

1) try: Inside, try block the statements having some exceptions' should be placed. The programmer, should place the statements having chances of exceptions inside it.
try block:
try {

//statements having exceptions

}
When, JVM detects the try block, so you program will be not be terminated. It stores the exception details in an exception stack and then JVM goes to catch block.

2). catch: try block should be always followed by catch block. In catch block, statements having explanation of errors or exceptions should be written. So, reason of exception will be displayed to user on their screen.
catch block:
catch(Exception e)
{
        //statements explaining reason of exception
}
You must be thinking what should come inside catch brackets. Firstly, there should be type of exception which may occur in program. If you don't know which type of exception may be there, you can simply write Exception there followed by variable.

Let us take an example in which we will perform Exception Handling:
public class Test {
public static void main(String[] args) {
int x;
int y;
x = 15;
y = 0;
int c = x / y;
System.out.println("c");
}
}
This program will give output as:
Exception in thread "main" java.lang.ArithmeticException: / by zero

💡As, we know that any number can't be divided by 0. Since, it is undefined in mathematic. So, it will through Arithmetic Exception.

Let us handle exception in above example by using try and catch block:
public class Test {
public static void main(String[] args) {
try {
int x;
int y;
x = 15;
y = 0;
int c = x / y;
} catch (ArithmeticException e) {
System.out.println("Any number can't be divided by 0.");

}
}
}
So, this program, by using try and block will give output as:
Any number can't be divided by 0.
Process finished with exit code 0
So, the exception can be handled by using try and catch block. So, here in this reason of exception has been displayed to user as shown in output.

Previous Post Next Post

Contact Form