标签:
package cn.seven.shengsiyuan.exception;
/*
* Time:2015年04月05日12:24:58,清明节
* Location:501
*
*/
public class ExceptionDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 3;
int b = 0;
int c = a / b;//这句话执行会发生异常,下面的语句便不执行
System.out.println("------");
System.out.println(c);
}
}
/*
Exception in thread "main" java.lang.ArithmeticException: / by zero
at cn.seven.shengsiyuan.exception.ExceptionDemo1.main(ExceptionDemo1.java:17)
*/
java.lang
Class Throwable
java.lang.Object
java.lang.Throwable
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Error, Exception
java.lang
Class Exception
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.Object
java.lang.Throwable-
java.lang.Exception
java.lang.RuntimeException
javax.management.JMRuntimeException
try{
}
catch(Exception e){
}
package cn.seven.shengsiyuan.exception;
/*
* Time:2015年04月05日12:24:58,清明节
* Location:501
*
*/
public class ExceptionDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int c = 0;
try{
int a = 3;
int b = 2;
c= a / b;
System.out.println("---hello---");
}
catch(ArithmeticException e){
//将19行产生的异常对象赋值给了e
e.printStackTrace();//printStackTrace打印异常的信息,来自Throwable类的方法
}
finally{
System.out.println("finally");
}
System.out.println("------");
System.out.println(c);
}
}
/*
* 19行出现异常,产生针对于该异常类的对象 ArithmeticException
* 当异常发生的时候,进入catch块,由catch进行接管,然后执行catch块中的内容
* 执行完毕,接下来执行syso语句
*
*
* 现在的重点便是21行-的catch语句的过程
*/
try{
//可能发生异常的语句的代码块
}
catch(异常对象){
//对发生异常之后的处理
}
finally{
//无论是否发生异常,最终都会被执行的语句,防止某些语句因为发生了异常而无法得到执行
}
package cn.seven.shengsiyuan.exception;
/*
* Time:2015年04月05日12:24:58,清明节
* Location:501
*
*/
public class ExceptionDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int c = 0;
try{
int a = 3;
int b = 0;
c= a / b;
System.out.println("---hello---");
}
catch(ArithmeticException e){
e.printStackTrace();//printStackTrace打印异常的信息,来自Throwable类的方法
}
finally{
System.out.println("finally");//一定会被执行,无论是否发生异常
}
//System.out.println(c);?疑问的是为什么这边会仍然输出的是 c=0?是因为c=a/b;语句出现异常,c没有执行赋值语句
}
}
Think in Java 笔记_Chapter12_1_Exception基础_继承和RuntimeException处理1
标签:
原文地址:http://www.cnblogs.com/together-sharing-for-freshman/p/4393998.html