Explain when code should throw an exception and when it should catch an exception:
Exception is an Runtime error thrown by the system.. So in few scenarios programmer might need to thrown an exception like when user inputs invalid data than in code we use the keyword throw and raise the exception manully
catch: We should catch the exception when ay of the statements inside the try is raising the exceptions or any where in the code we are throwing the exception
Example throw:
throw new NullPointerException();
Example catch:
try{
int a=10/0;
}
catch(ArithmeticException e){}
Get Answers For Free
Most questions answered within 1 hours.