20/02/25 ---------- Q)What is wrong with the following code? class A{ void x(int a) throws ClassNotFoundException{ if(a<10) throw new ClassNotFoundException(); } void z(){ x(20); } } Ans:- z() method should handle the checked exception, ClassNotFoundException or should have same throws clause. class A{ void x(int a) throws ClassNotFoundException{ if(a<10) throw new ClassNotFoundException(); } void z(){ try{ x(20); } catch (ClassNotFoundException e){ } } } OR class A{ void x(int a) throws ClassNotFoundException{ if(a<10) throw new ClassNotFoundException(); } void z() throws ClassNotFoundException{ x(20); } } Q)What is wrong with the following code? class A{ void x() throws ArithmeticException { int a=1,b=0,c; c=a/b; } void y(){ x(); } } Ans:- Nothing is wrong =>In the throws clause of x() method, unchecked exception is announced. Therefore, y() need not handle it. Q)What is wrong with the given code? class A{ void x(){ } } class B extends A{ void x() throws ClassNotFoundException{ } } Ans:- overriding method should not have a checked exception in its throws clause which is not announced in the overridden method. Q)What is wrong with the given code? class A{ void x(){ } } class B extends A{ void x() throws ArithmeticException{ } } Ans:- Nothing is wrong. As ArithmeticException is an unchecked exception. Q)What is wrong with the following code? class A{ void x()throws ClassNotFoundException{ } } class B extends A{ void x() { } } Ans:- Nothing wrong. overriding method need not announce any exception announced in the overridden method. Q)What are the things to be known about a Java method before calling it? 1)fully qualified name of the class to which it belongs. 2)signature of the method 3)return type 4)whether it is a static method or an instance method 5)accessibility mode of the method 6)its exception specification(throws clause) Q)Explain about user defined exceptions. =>Our own created exception i.e. programmer created exception class is nothing but a user defined exception. =>User defined exception class must inherit from java.lang.Exception or its sub class. For eg. class InsufficientFundsException extends Exception { }//user defined checked exception class AccountNotFoundException extends RuntimeException { }//user defined unchecked exception =>user defined exceptions also are classified into checked and unchecked type. =>JVM raises exception only when Java runtime environment rules are violated. But when user input violates our business rules(application rules), JVM doesn't raise the exception. In such cases, user defined exceptions are used to prevent the application flow instantaneously the moment our business rule is violated. For eg. AccountNotFoundException, InsuffcientFundsException