error has 2 types: compile time error and run time error or syntax error and logical error.
in logical error once the control face the error statement it creates the object of corresponding exception class. once exception object created the object passes to calling place of the method. once the object reject at JVM, JVM terminates the program by giving a exception message without for the execution.
without executing last statement if the program terminates that is known as abnormal termination.
to avoid abnormal termination we have to handle the exception.
All Exception class are the subclass of Throwable class. an exception can be of 2 types 1-simple exception 2 -serious exception
simple exceptions are subclass of Exception class.
serious exceptions are subclass of Error Class.
java advises not to handle serious exceptions. we can handle those which are from Exception class.
identify the exception object, catching it and handling it are known as Exception handling.
try and catch: java supports these 2 keywords to handle the exceptions.
the job of try keyword is to identify the exception object and pass the object to the catch. catch block will catch the exception object and handles it.
at time only one block will be execute: try or catch.
we can write n number of catch block to a single try but by revers is not possible.
throw keyword is used to throw an exception object, explicitly.
try... finaly: finally to force for doing some instruction. finally is not a method
the job of throws keyword is to transport Exception object from the method to the calling place.
in the calling place the end user has to handle the exception class object using the try and catch block.
the suggestion not to use throws keyword on main method because the calling place of main is JVM and once JVM will find the Exception object it will terminate the program.
when we put throws for a method, when we want to call it, we should call it in try and catch block.
Example: void meth(int x, int y) throws ArithmeticException
فریبا
سهشنبه 17 مردادماه سال 1391 ساعت 12:19 ق.ظ