测试类:
import junit.framework.TestCase; public class CoreTest extends TestCase { protected void setUp() throws Exception { super.setUp(); } public void testJisuan()throws yichang { Core core=new Core(); int a=core.jisuan(4, 0, 2); assertEquals(0,a); } }
计算类与异常类
public class Core { int result=0; public int jisuan(int a,int b,int c)throws yichang{ switch(c){ case 1: { result=a+b; break; } case 2: { result=a-b; break; } case 3: { result=a*b; break; } case 4: { try{ if(b==0){ throw new yichang(); } }catch(yichang e){ e.dengyu(); } result=a/b; break; } } return result; } } class yichang extends Exception{ public void dengyu(){ System.out.println("分母不能为0"); } }