标签:nbsp images rgs run 分享 http out log void
public class ExceptionDemo {
public static void main(String[] args) {
try {
//调用Checked异常方法,要么在main方法中再次抛出,要么显示的处理它
throwChecked(4);
} catch (Exception e) {
System.out.println(e.getMessage());
}
//调用抛出Runtime异常的方法,既可以显示的捕获它,也可以不理会该异常
throwRunTime(4);
}
/*
* 自行抛出Exception异常
* 该代码必须在try块中,或者在带有throws声明的方法中
*/
public static void throwChecked(int a )throws Exception{
if (a>0){
throw new Exception("a的值大于0");
}
}
/*
* 自行抛出Runtime异常
* 可以完全不理会该异常,也可以交给该方法的调用者处理
*/
public static void throwRunTime(int a){
if (a>0) {
throw new RuntimeException("a的值大于0");
}
}
}
运行的结果:

标签:nbsp images rgs run 分享 http out log void
原文地址:http://www.cnblogs.com/zydev/p/6815518.html