The purpose of Exception handling is to provide a means to detect and report an “Exceptional circumstance “,so that a proper action can be taken.
The process is as follows:
- Find the problem(Hit the exception)
- Inform that an exception/error has occurred (throw the exception)
- Receive the error information(Catch the exception)
- Take corrective actions(Handle the exception)
Errors are wrong/mistakes that can make a code go wrong.An error may produce an incorrect output or may terminate the execution.Errors can abruptly stop an execution or may crash the system.Errors can be divided into two catagories:
- Compile time error
- Runtime error
Compile time errors:
- Typing mistake
- Missing semicolons
- Missing or mismatches of brackets in classes or methods
- Misspelling of identifiers or keywords
- Missing double quotes in String
- Use of undeclared variables.
- Incompatible types in assignments/initialization.
- Bad references to objects
- Use of ‘=’ instead of ‘==’
- Javac: command not found.(Due to either java is not installed or path issue)
Error are referred as Runtime error and its sub classes are used for serious errors from which programs are not suppose to recover.They are not at all Exceptions. Errors are generally ignored during compilation time.
Exp-out of memory error.
If compiler displays an error it will not create a .class file.While runtime error occurs,program will stop execution after displaying error message.
Runtime errors are mainly due to the followings:
- Dividing an integer by zero.
- Accessing an element that is out of bounds of an array or String.
- Trying to store a value into an array of an incompatible type or class.
- Trying to cast an instance of a class to one of its subclases.
- Passing a parameter that is not a valid range or value for a method.
- Trying to illegally change the state of a thread.
- Attempting to use a negative size for an array.
- Using a null object reference as a legitimate object reference to access a method or variable.
- Converting invalid String to a number.
- Accessing a character that is out of bounds of a String. etc
Why force Checked Exceptions thrown by a method to be caught but not Unchecked Exception?
Checked Exceptions thrown by a method are part of the public programming interface.Those who invokes the method must be aware of them and should decide what to do with them.On the other hand, unchecked Exceptions are result of programming problem. It is usually not possible to recover from those problems.There could be numerous possibilities for Unchecked Exceptions,trying to cover all of them will reduce that code clarity.
Rule of Thumb while designing the public API that throws Exception:
- If the API user can take some meaningful action from the information available in the Exception object,We need to declare the Exception as checked.
- if the API user can not do anything meaningful after the Exception is thrown,We need to declare it as Unchecked Exception
Rule of Thumb with Exceptions Across Layers:
- A statement in a method can throw an exception that is not matched by a catch clause.This includes situations in which the statement throwing the exception is not even inside a try catch block.When this happens , execution of the program jumps out of the method that contains the faulty statements.Execution jumps back to whatever code called the method in the first place.
- A method can name more than one exceptions type in its throws clause. We need to use comma to separate these exception types.