异常的简单介绍

可自定义异常,如下所示:

@SuppressWarnings("serial")
public class MyException extends Exception{
private static final String exceptionMsg = "哎呀,我异常了";
public MyException(){
super(exceptionMsg);
}
public MyException(String message) {
        super(message);
    }
}

public class ExceptionTest {
public static void main(String[] args) {
try {
double result = getResult(10,0);
} catch(MyException e){

e.printStackTrace();

}

/**catch (Exception e) {

if(e instanceof MyException){

System.out.println("just test");

}
}*/

}
public static double getResult(int num1, int num2) throws MyException{
if(num2 == 0){
throw new MyException("不可以为0");
}
double result = num1/num2;
return result;
}
}

相关文章:

  • 2022-12-23
  • 2021-07-05
  • 2021-11-08
  • 2021-10-02
  • 2021-09-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
猜你喜欢
  • 2021-08-06
  • 2021-04-23
  • 2021-07-08
  • 2022-01-11
  • 2021-07-06
  • 2021-08-22
  • 2021-10-03
相关资源
相似解决方案