onecoders / Knowledge

Accumulation
0 stars 2 forks source link

How to create a custom exception type in Java? #46

Closed onecoders closed 10 years ago

onecoders commented 10 years ago

http://stackoverflow.com/questions/8423700/how-to-create-a-custom-exception-type-in-java

onecoders commented 10 years ago

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 }