19-02-25 ---------- Q)Explain about "throw" keyword. =>"throw" is one of the keywords of Java's exception handling mechanism. =>"throw" keyword is not used to create a block of code unlike try&catch =>"throw" keyword is used to raise the exception explicitly in the application by the programmer. For eg. throw new AccountNotFoundException(); OR AccountNotFoundException ae=new AccountNotFoundException(); throw ae; Note:- Exception object has to be created in order to throw it. Q)What do you know about rethrowing of an exception? =>Throwing an exception from within the catch block is nothing but rethrowing of an exception. For eg. catch(Exception e){ throw e;//retrhowing of an exception } =>To propogate an exception from one partition(layer) of the project to the other partition, the concept of exception rethrowing is used. Q)Explain about "finally" in the context of exception handling. =>"finally" is one of the keywords of Java's exception handling mechanism. =>"finally" keyword is used to create a block of code. =>Nature of finally block is that it gets excuted in exception raising case as well as non raising case. =>resources releasing code is placed in finally block. For eg. file closing database connection closing =>Generally, after the catch blocks finally block is written. try { ....... } catch(Exception e) { ...... } finally { //resources releasing } =>try block can be followed by finally block also try { ........ } finally { ...... } Q)Explain about "throws" keyword. =>"throws" is one of the keywords of Java's exception handling mechanism. =>"throws" is not used to throw the exception explicitly unlike "throw" keyword. =>"throws" is not used to create a block unlike try,catch,finally =>"throws" keyword is used to create "an exception specification" (throws clause) of a method. =>We can specify multiple exceptions also in throws clause. For eg. public void withdraw() throws AccountNotFoundException,InsufficientFundsException{ } =>"exception specification" of a method announces to the calling methods that this method has risky code in it but not handled. It is the responsibility of the calling methods to handle those announced exceptions. =>"throws" keyword purpose is to pass on exception handling duty from one method to the other.