Source Code
Source Code

Reputation: 288

Java If NoSuchElementException is returned

How can I make an if statement to check if "NoSuchElementException" is returned from a function? Something similar to what I have below.

if (functionReturns == "NoSuchElementException")

Upvotes: 3

Views: 1454

Answers (5)

Chetan Kinger
Chetan Kinger

Reputation: 15212

How can I make an if statement to check if NoSuchElementException" is returned from a function?

If you meant that your function returns a String with the value as NoSuchElementException, use equals instead of == :

if("NoSuchElementException".equals(functionReturns)) { }

If you meant that your function can throw a NoSuchElementException, use a try-catch. The catch block will be triggered when the function throws a NoSuchElementException.

try {
    function();
} catch(NoSuchElementException e) {
     //NoSuchElementException was thrown

}

If you meant that your function actually returns an instance of NoSuchElementException, you can use :

NoSuchElementException.class.isAssignableFrom(functionReturns)

Upvotes: 6

Source Code
Source Code

Reputation: 288

This way worked for me.

if(function.equals("NoSuchElementException"))

Upvotes: 0

Piyush Mittal
Piyush Mittal

Reputation: 1890

if you are using if statement then there must be thrown more than one error so in java 7 In Java 7 it was made possible to catch multiple different exceptions in the same catch block. This is also known as multi catch.

Before Java 7 you would write something like this:

try {

    // execute code that may throw 1 of the 3 exceptions below.

} catch(NoSuchElementException e) {//work as if if (functionReturns == "NoSuchElementException")
    logger.log(e);

} catch(NoSuchElementException1 e) {//work as if if (functionReturns == "NoSuchElementException1")
    logger.log(e);

} catch(NoSuchElementException2 e) {//work as if if (functionReturns == "NoSuchElementException2")
    logger.severe(e);
}

As you can see, the two exceptions NoSuchElementException1 and NoSuchElementException2 are handled in the same way, but you still have to write two individual catch blocks for them.

In Java 7 you can catch multiple exceptions using the multi catch syntax:

try {

    // execute code that may throw 1 of the 3 exceptions below.
//combind way to catch multiple error
} catch(NoSuchElementException1 | NoSuchElementException2 e) {
    logger.log(e);

} catch(Exception e) {
    logger.severe(e);
}

Notice how the two exception class names in the first catch block are separated by the pipe character |. The pipe character between exception class names is how you declare multiple exceptions to be caught by the same catch clause.

Upvotes: 0

AnkeyNigam
AnkeyNigam

Reputation: 2820

First of all NoSuchElementException or any other Exception is basically Thrown by a method not Returned. So you can/should not check it via return type. The best approach to handle any type of Exception is try catch blocks. Ex:-

 try {
   // Probable Exception Throwing code
} catch(NoSuchElementException e) {
     // handle/log specific type of error
}
catch(Exception e) {
     // handle/log Generic error if none specific is defined
}

Find more about Exception in the Official Docs here

Upvotes: 0

Dhruv Pal
Dhruv Pal

Reputation: 957

If method is throwing exception then simple use try and catch .

like

 boolean isException = false;
    try{
        //method that throws
    }catch(NoSuchElementException e){

      isException = true;
    //or perform what you like
    }

Upvotes: 1

Related Questions