Can we handle a error in java if yes than give an code of an example? Difference between error and exception handling.......
Exceptions are things you can create/throw yourself or that might be thrown because of an obvious run-time error such as trying to access a null object or reading an array out of bounds. They can also be caught and handled by a developer.
Errors should never be handled, thrown, caught, or repaired. They are usually beyond the scope of your application code such as 'out of memory'. Even if you caught an out of memory error, you wouldn't likely be in a better place than the system garbage collector to fix this (and you'd have to fix it while allocating no new memory) so its better to let the system crash and repair the root cause.
An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime.Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable.
While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.)
Ads