Java Exceptions:
Errors:
There are three types of errors.
- Compile Time Errors:Syntactical error found in the program due to which program fails to compile.Ex: Unable to compile if semicolon is missing end of the line.public static void main(String args[]) throws Excetpion{System.out.println(“Executed”)}
- Runtime Errors:
These errors represent
inefficiency of computer system to execute a particular program.
3. Logical Errors:
These errors depict flaws in the
logic .Program might be using the wrong formula.
Exceptions:
An exception is a runtime error.
Compile time errors are not exceptions. These are all errors.
All exception occurs at
runtime but some exceptions are detected at compile time and some
others at runtime. The Exceptions that are checked by the compiler at
compilation time are called Checked Exceptions. The
Exceptions are checked by JVM at the runtime are called unchecked
Exceptions.
+-------+
| Object | +--------+ | | +-----------+ | Throwable | +-----------+ / \ / \ +-------+ +-----------+ | Error | | Exception | +-------+ +-----------+ / | \ / | \ \________/ \______/ \ +------------------+ unchecked checked | RuntimeException | +------------------+ / | | \ \_________________/ unchecked
Unchecked
exceptions and errors are considered as unrecoverable and the
programmer cannot do anything whey they occur.
The Exceptions
occur at runtime but in case of checked exceptions whether you are
handling it or not it is detected at compilation time.
public
static
void
main(String args[]){
calFileReader();
}
public void
calFileReader() throws
IOException{
BufferedReader
in = new
BufferedReader(new
FileReader("file.xml"));
}
Exception Handling:
1. We can
handle the exceptions using try, catch and finally.
2. Handle
multiple exceptions in a method.
3. Only one
exceptions can handle at a same time.
Types of Exceptions:
There
are two types of exceptions.
1. Built
in exceptions
-
NullpointerException
-
ArithmaticException
-ArrayIndexoutboundException
-
NumberFormatException
2. User
defined exceptions
-
User defined exceptions are done by extending the Exception
class since Exception is the super class for Runtime
exceptions/Unchecked Exceptions.
public
class
MyException extends
Exception {
public
MyException() {
super();
}
public
MyException(final
String msg) {
super(msg);
}
}
No comments:
Post a Comment