Exception Handling purpose is to stop abnormal termination of a Program. It use to handle the runtime errors. Exception: अपवाद Error: ग़लती
Real world example of Exception and Error Once a Day you decided to go from Bihar to Patna and you start your car drive towardsbridge (means you are in executing state) means (you program start executing) and you are driving at speed 60 then suddenly some guy used to cross road and you get stuck a car to him.but you never know that anyone try to cross the busy road its not usual situation because no one try to cross bussiest road. So its exceptional case when a guy cross that road.so but you at any how save him as well himself by take out his bike from side. It means he handle the situation and he said plz walk carefully on road don’t try to cross like this otherwise you will dead soon Pagal.and this is called exception handling .means at runtime any exception which you didn’t determine that it can come so its basically to stop terminating the program at abnormal condition. Error means if he get stuck with that guy who was crossing road and both injured then its called an error. Error can’t be handled but exception can handle. Error: syntax ,logical,runtime.
Syntax Error :when you missed to place any semicolon,or braces etc Logical Error : forget to pass argument in method. Runtime Error: like we divide number from 0 .
Exceptional handling is the process of handling runtime error.
Exceptional handling is the process of handling runtime error.
When you write above program and try to run the program then program will compile happily but you forget to give value at run time then you will get following error at runtime.
But when you give value 10,0
because any no can’t be divide with 0; when you give an correct input then 10,2 and we are try to access the arr[2] which is not there. So then we will get an error.
ClassNotFoundException,NullPointer Exception and many more Exceptions
To handle this situation we should use Try catch block.in Try block contains those set of instruction where exception may arise.but not write a normal statement into try block.try must be with catch Block or Finally but one of them must be with try block.try must be followed by catch between them no set of instruction but spaces allowed. and rest of the instruction below catch block.
If you first write Exception then NullPointerException then you will get Compile time error.
When we don’t use any kind of try catch block and when exception arises
int i=10/0;
then jvm by default add the try catch handler in the statement,jvm just give this to provide only for display message.but why its not handled it because its not have much capability to maintain the flow of your program.
Sol: when exception occur then jvm decide which kind of exception is this load that exception class at runtime and throw that class object in our program if corresponding catch is there in our program then exception handle otherwise not.
Finally block is always executing block means if u have an exception then finally block executed or if we havn’t exception still its execute.
Sol: Inside this block we write cleanup code of the programs. Cleanup code is you want always execute like closing database connection, filestream closing statement, socket closing statement.
But some time its communicate with external resources and suppose we create a connection with database and programs terminate and connection not closed then at that time the connection may be Misused or may this connection effect another program so to close the connection we should always write connection closing statement inside finally block because its always exceuting whether exception arise in a program or not.
Sol: When you forcefully terminate the program like System.exit(0);
Method return statement is also can’t stop to run finally block.
Throw keyword
Internally when exception arises in try then jvm checks the exception and goes to the corresponding class and map the excpeti Ion and create the object of the class is the responsibility of JVM and The object created at runtime and throw into our program by using Throw keyword. Explicitly we can also do this we can create an object of our own excpetion and can throw when its require. JVM excpetion throws when abnormal situation arises but we want in normal situation like if user age < 18 then exception occurred.
If we want to throw exception explicitly or forcefully in the program then we can use throw keyword.or we can use in custom excpetion.
The main purpose of throw keyword is throw the object of custom exception.
In every Exception class Whether its checked or unchecked there is 2 constructor available 1 is default and another string type.
Sol: Calling method time, throws is a keyword which indicates with method signature like this public void write() throws IOExcpetion to the end user that what type of excpetion may occur in your program.Generaly we thows checked excpetion.
Sol: Checked Exception are known by the compiler at compile time Unchecked excpetion are known by the jvm at runtime.