Write your own Java exception class combat

A Code

public class userDefinedException
{
   public static void main( String[] args )
   {
      try{
         throw new MyException( "自定义异常" );
      }
      catch( MyException e ){
         System.out.println( e );
      }
   }

}

class MyException extends Exception
{
   public MyException( String msg )
   {
      super( msg );  // 调用Exception类的构造方法,存入异常信息
   }
}

Two runs

MyException: custom exception

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/94957380