自定義異常的使用
package ImoocException;
public class my_Exception extends Exception {
/**
*?
*/
private static final long serialVersionUID = 1L;
public my_Exception() {
System.out.println("使用了這個(gè)自定義的類1!");
}
public my_Exception(String str) {
super(str);
System.out.println("使用了這個(gè)自定義的類2!");
}
public void devide(int a,int b) throws Exception {
if(b==0) {
throw new my_Exception("被除數(shù)不能為0");
}
else {
System.out.println("it is ok!");
}
}
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
ImoocException ?imooc=new ImoocException ();
try {
imooc.devide(10, 0);
}
catch(my_Exception e) {
? ?System.out.println("異常拋出!");
}
}
}
最后結(jié)果還是出來(lái)
Exception in thread "main" java.lang.Exception
at ImoocException.ImoocException.devide(ImoocException.java:6)
at ImoocException.my_Exception.main(my_Exception.java:29)
這是怎么回事?
2018-05-26
public static void main(String[] args)throws Exception 這里拋出異常??
mian方法是程序的起點(diǎn)? 在拋出只能交給JVM處理? 相當(dāng)于自己沒(méi)有捕獲異常? 沒(méi)有寫(xiě)try---catch
2018-05-06
public static void main(String[] args)throws Exception
這里不需要拋異常了吧
public void devide(int a,int b) throws Exception 是不是要改成這樣
public void devide(int a,int b) throws my_Exception