標籤:
Class RuntimeException
- java.lang.Object
- java.lang.Throwable
- java.lang.Exception
- java.lang.RuntimeException
- ArithmeticException
- IndexOutOfBoundsException
- ……
public class ExceptionTest { public static void main(String[] args){ DivDemo dd = new DivDemo(); int resultD; resultD = dd.div(10, 0); System.out.println("resultD‘ value is :"+resultD); System.out.println("over!"); }}/**-----------------Class ArithmeticException * java.lang.Object * java.lang.Throwable * java.lang.Exception * java.lang.RuntimeException * java.lang.ArithmeticException * */ class DivDemo{ /**RuntimeException的特點 * RuntimeException <-- ArithmeticException * 方法內部向jvm拋 <RuntimeException>異常 * 方法名後面可不加throws xxxException,即可不聲明這個函數體會拋出異常,不讓調用者處理,就是要讓程式停掉。 * * 而調用該方法的程式員對異常進行不了處理,只能對程式更改以符合傳入參數的合法性。 * */ public int div(int a,int b){ if(b == 0 ){ throw new ArithmeticException("----by zero!"); } return a/b; }}
:console:
--------------------------------------------------------------------------
Get
Java基礎--異常—RuntimeException