Exceptions or OCA Flashcards
how many types of exceptions to be recognized
runtime exceptions, checked exceptions and errors
what is exception
is an event alters the program flow, un wanted event
when occur in execution the program
exceptions are objects?
yes
behind the exceptions generating?
- object created by the JVM
2. thrown to the program
throw keyword?
is used throwing the exception manually, throw aib(object name)--->JVM by using this keyword we can see the exception code in councel.
how could be prove that exceptions are class and throw keyword
for ex; ArithmeticExceptions art= new ArithmeticExceptions(); throw art; // JVM how throw it to program.....
what is the try keyword—-(try catch block)
try {
some code written} catch (name of the exceptions subject name)
if we change the catch- with other exceptions does it work?
no it does not catch anything,-so IndexOutOfBOund Exceptions- not catch the Arithmetics Exceptions
can I add second catch block-multi catch buyer?
yes, it is goingto be same, catch() System.out…
catch()System out….
what is the more one to cover all exceptions like arithmetics, indexoutof bound…
Exception—instead of arithmeticsExceptions, replace the exceptions…
how many types exceptions
uncheked exceptions(runtime exceptions) checked exceptions(Exception and subclasses)
handle or declare rule
Java requires the code either handle or declare in the method signature
difference between Errors and Exception?
Errors indicate serious problems that does not try and catch, Exceptions indicates the situation that can try and catch_ both is child class of throwable
Exceptions—Checked?runtime exceptions
Checked exception must be handled before we can run the program. If ignored then program won’t compile.
Unchecked Exceptions/Runtime Exception
Type of exception where will be thrown during runtime.
It is very challenging to predict. Because it won’t complain during compile time.
how handle exceptions
throws Exception,
try and catch block
multiple Catch block rules
When using multiple catch blocks, Parent Exception type MUST be used in the end. Otherwise child exception catch blocks will be ‘Unreachable code’ and it won’t be compiled.
Use pipe line ”|” between exceptions when throwing multiple exceptions.
if there are multiple catch block, when exception is caught by first catch block, rest of the catch block will NOT catch any exception.
difference between throw and throws?
throw –> explicitly creating exception(tell Java that u want throw the exception
throws –> passing the exception to next user to handle(that the method might throw an exception
adding finally block
try{//code} catch(exception identifielder){} finally{}
if exceptions is thrown finally block run after catch block;
no exceptioons is thrown after try block complete
Run time exceptiosn
ArithmeticExceptions/IndexOutofBound Exception/NullPointerException/
Checked Exceptions
IOException/FIleNotFoundException
Errors
Errors extend the Error class. StackOverFlowError,NoClassDeFoundError/
sample for writing throw keyword
throw new Excep(); throw new Excep("oh hey");
how to read the code
start top try block and ctach if there is any exceptions of not pass to the finally blocks