Which is the best way of exception handling?

Answers were Sorted based on User's Feedback



Which is the best way of exception handling?..

Answer / hari

instead of using try{}catch{} blocks throw an
ApplicationException and make suclass to that class and
extend RuntimeExceptions to ApplicationException class

public void read(String name)throws ApplicationException{
badurl(name);
numberFormate(name);
}
public void badUrl(String name)throws BadUrlException{
}
public void numberFormate(String name) throws
BadNumberException{
}
ApplicationException extends RuntimeException{
}
BadUrlException extends ApplicationException{}
BadNumberException extends ApplicationException{}

Is This Answer Correct ?    16 Yes 1 No

Which is the best way of exception handling?..

Answer / sudhir dhumal

First best way is use built-in exception classes wherever possible, you don't need to create your custom exception class to represent every scenario, so try to use existing exception classes

Second best way of handling exception is creating custom exception for specific scenario by extending our class with Exception class.

Consider the following example...

public class BookSearchException extends Exception {
public BookSearchException(String message) {
super(message);
}
}

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are the important features of Java 9 release?

0 Answers  


When parseint method can be used?

0 Answers  


What are the rules for naming an array?

0 Answers  


Why we override equals() method?

0 Answers  


Can a class extend abstract class or interface? why

4 Answers   Wipro,






What is set in java?

0 Answers  


What do you understand by overloading and overriding in java?

0 Answers  


where is .equals() method and how is it different from == operation <giving some confusing implementation> Is hashing related to these?

4 Answers   iFlex,


What method is used to specify a container's layout in java programming?

0 Answers  


Explain about core java?

0 Answers  


Explain java heap space and garbage collection?

0 Answers  


Can we convert stringbuffer to string?

0 Answers  


Categories