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 the range of the short type?

0 Answers  


Can you make a constructor final in Java?

0 Answers   SwanSoft Technologies,


How do I compare two strings in word in java?

0 Answers  


Is it possible to instantiate the abstract class?

0 Answers  


How does arraylist size increase in java?

0 Answers  






can we take more than one null values for Unique constraints.

1 Answers   3i Infotech,


How many bits are allocated to represent character of character sets - Unicode, ASCII, UTF-16, UTF-8?

1 Answers  


Can a constructor have different name than a class name in java?

0 Answers  


What is the difference between static and non-static variables?

6 Answers  


Can private members of a base class are inheritable justify?

0 Answers  


Is hashset ordered java?

0 Answers  


What are Encapsulation, Polymorphism and Inheritance?

5 Answers  


Categories