Concept of Exception Handling Using try-catch
Exception is a type of runtime error while execution of program which user can possibly handle (catch) and report with a suitable message or take some other planned action.
for e.g. divide by zero (Arithmetic exceptions), array out of bound (ArrayIndexOutOfBoundsException), file not found (FileNotFoundException)
Exception can be handled by using the try catch syntax in java as follows.
class ArithEx
{
public static void main(String[] args)
{
int a = 10, b=0, s;
try{ //This block contains code where exception is excepted to occur
s=a/b; //Here divide by zero scenario is expected to occur
}
catch (ArithmeticException e) { //This block has code which runs when exception occurs
System.out.println("Divide By Zero Error");
}
}
}