/** * Created by Administrator on 2014/9/29. * * throws和throw的區別 * * throws使用在函數上,throw使用在函數內 * * throws后面跟的是異常類,可以跟多個,用逗號連接 * throw后面跟的是異常對象 */ //copyright?liupengcheng //http://www.49028c.com/liupengcheng class fushuException extends Exception { PRivate int value; fushuException(String msg,int value) { super(msg); this.value = value; } public int getValue() { return value; } }
//copyright?liupengcheng //http://www.49028c.com/liupengcheng
class Demo5 { int div(int a,int b) throws fushuException //fushuException 自定義負數異常 { if(b<0) throw new fushuException("出現了除數是負數的情況",b); return a/b; } }
//copyright?liupengcheng //http://www.49028c.com/liupengcheng
public class ExceptionDemo3 { public static void main(String [] args) { Demo5 d = new Demo5(); try { int x = d.div(4,-1); System.out.println("x"+ x); } catch(fushuException e) { System.out.println(e.toString()); System.out.println("出現問題的數值是"+ e.getValue()); }
System.out.println("over"); } }
//copyright?liupengcheng //http://www.49028c.com/liupengcheng
新聞熱點
疑難解答