异常体系(Exception 和Error)
*Throwable 是Exception Error的共同父类
*Exception
* 1.编译时异常 编译时必须处理的异常
* FileNotFoundException
* 2.运行时异常 运行时出现的异常
* NullPointerException 一个为Null的东西 调用方法或属性
* ArrayIndexOutOfBoundsException 索引超出指定的范围
* ClassCastException 发生在强制转型
* ArithmeticException 除0时出现的异常
* IllegalArgumentException 参数非法
*
* 处理异常:
* 1.try catch(不会同时执行) finally(一定会执行)
* 2.throws(不负责任的抛掉)
* 自定义异常:throws 自定义的异常类的对象
*
* Error:错误 程序设计问题
* OutOfMemoryError 堆内存溢出
* StackOverflowError 栈内存溢出
*
*/
java.lang.ArithmeticException: / by zero
at com.nanhua.test.Test.main(Test.java:33)
除数不能为0
catch+++++++
finally_______
*Throwable 是Exception Error的共同父类
*Exception
* 1.编译时异常 编译时必须处理的异常
* FileNotFoundException
* 2.运行时异常 运行时出现的异常
* NullPointerException 一个为Null的东西 调用方法或属性
* ArrayIndexOutOfBoundsException 索引超出指定的范围
* ClassCastException 发生在强制转型
* ArithmeticException 除0时出现的异常
* IllegalArgumentException 参数非法
*
* 处理异常:
* 1.try catch(不会同时执行) finally(一定会执行)
* 2.throws(不负责任的抛掉)
* 自定义异常:throws 自定义的异常类的对象
*
* Error:错误 程序设计问题
* OutOfMemoryError 堆内存溢出
* StackOverflowError 栈内存溢出
*
*/
public class Test{
public static void main(String[]args)throws FileNotFoundException{
try{
int a=10/0;
System.out.println("try--------");
}catch(ArithmeticException e){
e.printStackTrace();
System.out.println("除数不能为0");
System.out.println("catch+++++++");
}finally{//最后 始终都会执行 关闭资源
System.out.println("finally_______");
}
程序运行结果
<pre name="code" class="java">
java.lang.ArithmeticException: / by zero
at com.nanhua.test.Test.main(Test.java:33)
除数不能为0
catch+++++++
finally_______
//创建一个文件输入流对象
<span style="white-space:pre"> </span> /**
<span style="white-space:pre"> </span> * x:\\1.txt可能不存在电脑上
<span style="white-space:pre"> </span> *
<span style="white-space:pre"> </span> */
//Text text=new text();
<span style="white-space:pre"> </span> //text.read();
<span style="white-space:pre"> </span> int[]array=new int[10000000];
<span style="white-space:pre"> </span> //垃圾自动回收机制 工作 拓展(数组空间过大,超出内存范围)
<span style="white-space:pre"> </span> while(true){
<span style="white-space:pre"> </span> new JButton("");
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> // Text text=new Text();
<span style="white-space:pre"> </span> // text.study();
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span> //死递归:没有结束条件的递归,找不到出口(栈溢出的情况)
public void study(){
<span style="white-space:pre"> </span>int i=0;i++;
<span style="white-space:pre"> </span>//输出结果
<span style="white-space:pre"> </span>//使用计数器 计算栈的深度?
<span style="white-space:pre"> </span>study();
}
public void read() throws FileNotFoundException{
<span style="white-space:pre"> </span>FileInputStream fis=new FileInputStream("x:\\1.txt");
<span style="white-space:pre"> </span>
}
<span style="white-space:pre"> </span> }