Here is an example of a defined custom exception, developers can serve as a reference, write your own exception classes according to project requirements
Package com.thinkgem.wlw.modules.job.service; // custom exception the Exception class to inherit public class MyException the extends Exception { / ** * It should be a non-reference and a constructor argument constructor * / public MyException ( ) {} public MyException (String S) { Super (S); } / ** * a class definition may throw exception process, this method can be written in a single class after * define the method, others can It is performed using the * @param FEN * @return * @throws MyException * / public String deiFen ( int FEN)throws MyException { IF (FEN> = 0 && FEN <= 100 ) { return "normal" ; } the else { // when throws a custom exception scores outside the range of 0 to 100 the throw new new MyException ( "Error Input" ); } } / ** * test * @param args * / public static void main (String args []) { the try { MyException m = new new MyException (); System.out.println (m.deiFen ( 123 )); @ 123 is not within the range, abnormality occurs
} The catch (Exception E) { System.out.println ( "exception information is:" + e.getMessage ()); } } }
Test Results: