What are the ways you can handle exception ?

What are the ways you can handle exception ?

When an exception is thrown by a method, it must declare or handle it. 

 A method can declare the exception using the keyword throws with the method signature and a list of exceptions type so that the calling methods can guard themselves. 

 For example: 

public void myMethod() throws IOException, ArithemeticException{//some code here that might cause the exception}If a method does not declare the exception then it must handle the exception by providing the try/catch block.For example:public void myMethod(){try{//some code here that might cause the arithematic exception}catch(ArithematicException e){}}


By:Jalees
Date:2011-01-09 00:00:00

When an exception is thrown by a method, it must declare or handle it. 

 A method can declare the exception using the keyword throws with the method signature and a list of exceptions type so that the calling methods can guard themselves. 

 For example: 

public void myMethod() throws IOException, ArithemeticException{//some code here that might cause the exception}If a method does not declare the exception then it must handle the exception by providing the try/catch block.For example:public void myMethod(){try{//some code here that might cause the arithematic exception}catch(ArithematicException e){}}


By:Sumit Tripathi
Date:2018-05-15 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher