1

Please see my custom exception file (ExceptionHandler.java) below:

package utilities;


public class ExceptionHandler {
    public void browserException() throws BrowserException {
        new BrowserException();
    }
    public void userInputException() throws UserInputException {
        new UserInputException();
    }
    public void timeOutException() throws TimeOutException {
        new TimeOutException();
    }
    public void notFoundException() throws NotFoundException {
    new NotFoundException();
    }
}

//////////////////// Exception List Here ////////////////////
@SuppressWarnings("serial")
class BrowserException extends Exception {
    public BrowserException() {
        System.out.println("Unhandled Browser!");
    }
}

@SuppressWarnings("serial")
class UserInputException extends Exception {
    public UserInputException() {
        System.out.println("Invalid user-input!");
    }
}

@SuppressWarnings("serial")
class TimeOutException extends Exception {
    public TimeOutException() {
        System.out.println("Script timed out!");
    }
}

@SuppressWarnings("serial")
class NotFoundException extends Exception {
    public NotFoundException() {
        System.out.println("Element Not Found!");
    }
}

I'm trying to call this ExceptionHandler-class as shown-below, but getting errors like "No exception of type void can be thrown; an exception type must be a subclass of Throwable".

throw new ExceptionHandler().browserException();

Kindly anyone let me know, if at all it is a valid way. If not, then what all could be the best possible approaches to group multiple custom-exception classes? Please bear with me as I'm a novice in Exception Handling.

Many thanks!

abhi17
  • 33
  • 6
  • You should return the `new BrowserException` instead of throw in the method `browserException()` for your usage. Or throw the exception in the method and change to `new ExceptionHandler().browserException()` without `throw`. – Dean Xu Nov 04 '17 at 02:51

1 Answers1

0

You should call

throw new BrowserException

baliman
  • 588
  • 2
  • 8
  • 27
  • Another question. Which import statement should I use in this case?... (a) import utilities.ExceptionHandler; OR (b) import utilities.BrowserException; Please note- "ExceptionHandler" is the public class and "BrowserException" is non-public class. – abhi17 Oct 14 '17 at 18:55
  • Also is there anymore need of keeping the methods- browserException(), userInputException(), timeOutException(), notFoundException() inside the ExceptionHandler-class??? I do not find them having value anymore. – abhi17 Oct 14 '17 at 19:03
  • You can remove the methods it is useless. Your ide helps you to organize imports – baliman Oct 14 '17 at 19:28