Java异常捕获的格式:try...catch...finally
代码如下:专注代码块好几天:
class MyMath
{ //此时表示div()方法上如果出现了异常交给被调用处去处理
public static int div(int x ,int y) throws Exception {
int result = 0 ;
System.out.println("***1、除法计算开始 ***") ; //可以理解为把门打开
try
{
result = x/y ;
}
catch (Exception e)
{
throw e ; //继续抛异常
}finally{
System.out.println("***2、除法计算结束 ***") ; //可以理解为把门关上
}
return result ;
}
}
public class TestDemo
{
public static void main(String args[]) {
try
{
System.out.println(MyMath.div(10,0)) ;
}
catch (Exception e)
{
e.printStackTrace() ;
}
}
}
运行结果如下: