I am taking a Java course. So I am just learning Java. The textbook says to either extend the Exception class or extend the RuntimeException class for a custom exception. I understand RuntimeException is a subclass of Exception.
I have a homework assignment to implement the bin2Dec method to throw a custom exception if the string is not a binary string.
I found the exercise worked out by somebody and was studying their solution. However, I see that they extended the NumberFormatException class which of course is a child of IllegalArgumentException which is a child of RuntimeException.
This led me to wonder why they decided to use NumberFormatException? I assume I can work out a solution simply using Exception or RuntimeException. Especially since that is all that is covered in the text.
I tried searching the web to find answers to this question but all I found was several places stating basically what the book says (extend Exception or RuntimeException) and explaining (as the book does) when to use one vs the other.
All I was able to find was several articles saying that you "can" use a more appropriate subclass like NumberFormatException but no explanation of how a person would come to that conclusion when setting out to make a custom exception. I found one article saying extending NumberFormatException is almost always a bad idea and explained several reasons why but then it said IllegalArgumentException would likely be more appropriate. This leaves me wondering the same question... What would lead me to decide to use IllegalArgumentException instead of Exception or RuntimeException.
bin2Decmethod is supposed to return anint, then its wrongly named. There is nothing decimal about anint. Anintis just a bit pattern. The "decimal" is just a string representation. If the method really means "binary to decimal" then it should return a decimal String.