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 is synchronization? How it can be achieved?

2 Answers  


What is size of int in java?

0 Answers  


what is dynamic method dispatch ?

27 Answers   Fidelity, MUET, SDSF,


what us Dyna action form?

4 Answers   Aithent Technologies,


How do you replace a string in java?

0 Answers  






What is a method in programming?

0 Answers  


What is the integer of 16?

0 Answers  


what is the use of datasource in core java?

3 Answers   Satyam,


Why string objects are immutable in java?

0 Answers  


Can we override final method?

0 Answers  


How can a class be accessed, If no access modifiers are declared?

4 Answers  


What is meant by oops concept in java?

0 Answers  


Categories