UnsupportedAddressTypeException is a subclass of RuntimeException, and from the JavaDoc:
Answer from Jonathan on Stack OverflowRuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
Videos
UnsupportedAddressTypeException is a subclass of RuntimeException, and from the JavaDoc:
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
If your exception extends java.lang.Exception, you must catch it (or rethrow). If it extends java.lang.RuntimeException, you are not required to do so. You will find that this is true for all standard exceptions as well.
edit Changed the words must not to not required to
You should be able to create a custom exception class that extends the Exception class, for example:
class WordContainsException extends Exception
{
// Parameterless Constructor
public WordContainsException() {}
// Constructor that accepts a message
public WordContainsException(String message)
{
super(message);
}
}
Usage:
try
{
if(word.contains(" "))
{
throw new WordContainsException();
}
}
catch(WordContainsException ex)
{
// Process message however you would like
}
You need to create a class that extends from Exception. It should look like this:
public class MyOwnException extends Exception {
public MyOwnException () {
}
public MyOwnException (String message) {
super (message);
}
public MyOwnException (Throwable cause) {
super (cause);
}
public MyOwnException (String message, Throwable cause) {
super (message, cause);
}
}
Your question does not specify if this new exception should be checked or unchecked.
As you can see here, the two types are different:
Checked exceptions are meant to flag a problematic situation that should be handled by the developer who calls your method. It should be possible to recover from such an exception. A good example of this is a FileNotFoundException. Those exceptions are subclasses of Exception.
Unchecked exceptions are meant to represent a bug in your code, an unexpected situation that you might not be able to recover from. A NullPointerException is a classical example. Those exceptions are subclasses of RuntimeException
Checked exception must be handled by the calling method, either by catching it and acting accordingly, or by throwing it to the calling method. Unchecked exceptions are not meant to be caught, even though it is possible to do so.
Java provides 2 types of exceptions : Checked and Unchecked.
Checked once are the those which the compiler will prompt you too handle (in a try-catch block) while for the unchecked exceptions compiler will not ask you to handle them.
RuntimeException class is the top level class for Unchecked exceptions and hence if your custom exception classes extends RuntimeException class then automatically compiler will not prompt you to handle them explicitly in a try-catch block.
Basically RuntimeExceptions are those which java considers as occurring at the execution time and anything occurring at the execution time like NullPointerException cannot be handled, hence we cannot do much at the compile time.
If you don't want to force your code to wrap these methods in try blocks, why are you having them throw exceptions?
Let's consider these methods:
public void badMethodOne() throws CustomExceptionOne;
public void badMethodTwo() throws CustomExceptionTwo;
You're asking how you can avoid this scenario:
public void myMethod(){
try{
badMethodOne();
}
catch(CustomExceptionOne ex){
ex.printStackTrace();
}
try{
badMethodTwo();
}
catch(CustomExceptionTwo ex){
ex.printStackTrace();
}
}
One option would be to simply catch a superclass Exception:
public void myMethod(){
try{
badMethodOne();
badMethodTwo();
}
catch(Exception ex){
ex.printStackTrace();
}
}
You might also use a multi-catch block:
public void myMethod(){
try{
badMethodOne();
badMethodTwo();
}
catch(CustomExceptionOne | CustomExceptionTwo ex){
ex.printStackTrace();
}
}
You might also pass responsibility "up the chain" and let whoever's calling your code handle the try/catch:
public void myMethod() throws CustomExceptionOne, CustomExceptionTwo{
badMethodOne();
badMethodTwo();
}
I don't really work with exception handling a lot, so I'm out of my comfort zone for this assignment. In essence, the code I'm testing is a List object, checking it to see if the object I want to add already exists within the list, and throwing a custom exception if it does. I'm using junit testing, if that matters.
public void addConnection(LinkedInUser user) throws LinkedInException
{
try {
for (int x = 0; x < connections.size(); x++)
{
if (connections.get(x) == user)
throw new LinkedInException();
}
//if (connections.contains(user))
// throw new LinkedInException();
//else
connections.add(user);
} catch (LinkedInException e) {
e.Exception("You are already connected with this user");
}
}The commented portion was the old code. I think the problem is determining whether or not the object is already within the list, but I could be wrong. The LinkedInException class is just an empty class. Our instructor told us to override the five Exception class constructors, but that was it, so the overrides are blank.
Here's the junit test code
@Test
public void errorCheckAddCon()
{
LinkedInUser test = new LinkedInUser("han", "password123");
LinkedInUser test1 = new LinkedInUser("luke", "password123");
boolean exceptionThrown = false;
try {
test.addConnection(test1);
test.addConnection(test1);
} catch (LinkedInException e) {
exceptionThrown = true;
}
Assert.assertTrue(exceptionThrown);
}(I think it's the addConnection method, and specifically the List.contains method within, because I manually set boolean exceptionThrown to true, and the junit test passed, so the junit test code isn't the issue here I believe)