java的异常知识:
一:
(1):特殊值带回异常
(2):异常与捕获
二:
->java 异常的种类
-> Throwable 可抛出的
->Error 错误,系统不可恢复的错误
->堆内存溢出错误
->java 有内存泄漏:
测试程序:
public class Demo4 { public static void main(String[] args) { String[] ary=new String[500000000]; for(int i =0 ;i<args.length;i++){ String s=new String(i+"早晨好"); ary[i]=s;//引用一直在,不进行回收机制 s=null; } } }
测试结果:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
提示:字符串使用之后,及时释放引用,垃圾回收机器会释放对象占用的内存,程序就可以正常运行。
->Exception 例外,异常
-IOException
-RuntimeException:非检查异常
三 ->try....catch
程序出现异常,Java异常机制就去寻找catch语句,如果不使用catch处理,当前的线程就会出现停止,在程序中要有合理的catch处理
语法:
try{//可能出现错误的代码段
}catch{//出现异常的处理方法1
}catch{//出现异常的处理方法2
}finally{//最终处理,无论是否有异常
}
经典案例->1:
/*
* 将一个字符串的第一个转换为整数
*/
public class Demo5 { public static void main(String[] args) { try{ String str="";//null char c=str.charAt(0); int n=c-'0'; System.out.println(n); } catch(StringIndexOutOfBoundsException e){ System.out.println("没有字符串"); }catch(NullPointerException e){ System.out.println("不能为空!"); }finally{ System.out.println("最后完成"); } } }
经典案例->2:(关于复制文件比较完善的程序,加了异常处理)
<span style="font-size:18px;">import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo6 { public static void main(String[] args) { //将in和ou放到try外,这样,finally 中就可以使用 FileInputStream in=null; FileOutputStream ou=null; try{ in = new FileInputStream("ddqq.txt"); ou=new FileOutputStream("wwqwwq.txt"); int n; byte[] buf=new byte[1024*8]; while((n=in.read(buf))!=-1){ ou.write(buf,0,n); } }catch(FileNotFoundException e){ System.out.println("没有找打文件"); }catch(IOException e){ System.out.println("读写故障"); }finally{ //用于可靠的释放资源 try{ if(in!=null) in.close(); if(ou!=null) ou.close(); }catch(IOException e){ //文件关闭异常 } } } }</span>
经典案例->3
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 封装一个算法,实现文件的复制 * 异常的传播处理 * @param in 源文件 * @param out 目标文件 * @throws 复制失败时抛出异常,当计算方法执行意外情况是 * 抛出异常作为意外的结果,使用throws * 说明方法可能会有异常发生 * * @author soft01 * */ public class Demo8 { //用不到当前数据,参数是后面算法必须的条件 public static void cp(String src,String dis)throws IllegalArgumentException{ //复制失败异常 FileInputStream in=null; FileOutputStream out=null; try{ in=new FileInputStream(src); out=new FileOutputStream(dis); int n; byte[] buf=new byte[1024*8]; while((n=in.read(buf))!=-1){ out.write(buf,0,n); } }catch(FileNotFoundException e){ System.out.println("文件没找到!"); //抛出异常,通知调用者,当前算法发生了意外情况 throw语句结束了当前执行方法 throw new IllegalArgumentException("没有文件"); }catch(IOException e){ System.out.println("读写失败哦!"); }finally{ try{ if(in!=null) in.close(); if(out!=null) in.close(); }catch(IOException E){ } } } } // 2.调用方法操作是否正确:
public class Demo9 { public static void main(String[] args) { Demo8.cp("ww","rew.txt"); System.out.println("复制成功"); } }
四:非检查异常:Java编译器不检查RuntimmeException及其子类的异常处理规则,不报编译错误
其他异常:当调用有异常抛出的放大,必须处理异常,否则出现编译错误!
测试代码:RuntimeException
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.SimpleFormatter; //若输入Date错误,则输出当前的系统时间。若给的Date是正确的,则输出所给时间。 public class Demo10 { public static void main(String[] args) { /*NumberFormatException * 是RuntimeException 的子类型,编译器不检查 */ String str="2016-2-2"; SimpleDateFormat sf=new SimpleDateFormat("yyyy-M-dd"); /* * ParseExceptions 是“可检查异常”,编译器检查语法: * 如果方法抛出可检查异常,则必须处理异常,否则编译错误 * 强迫程序员,必须进行适当的意外错误处理,避免运行期间麻烦 */ Date date=new Date(); try{ date =sf.parse(str); }catch(ParseException e){ e.printStackTrace(); } System.out.println(date); } }
五:常见异常:RuntimeException
(1)NullPinterException:当一个引用类型变量其值为空的时候。访问了其的方法和属性是时,就会出现空指针异常。
(2)IllegalArgumenntException:传递了一个不合法的不正确的异常
(3)ArrayIndexOutBountsException:数组越界
(4)ClassCastException:强转异常
(5)NumberFormatException:字符串转换异常
六:异常的信息:
Exception 常用的API
1 printStackTrace
Throwable 中定义了一个方法可以删除错误信息,用来跟踪异常事件发生时堆栈的内容。该方法定义为:
-void printStackTrace()
Try{
...
}catch(Excpetion e){
e.printStackTrace();/输出执行的堆栈信息
}
2 getMessage
Throwable 中定义了一个方法可以得到有关异常时间的信息。该方法定义为:
-String getMessage()
Try{
...
}catch(Excpetion e){
System.out.println(e.getMessage());}
3 getCause
这个方法是检索导致异常的原因,这些可以对异常根层次的原因提供更多的信息。
Throwable getCause()
获取该异常出现的原因
七:自定义异常:
JAVA异常机制可以保证更加安全和更加健壮,为了更加准确的捕获异常所以提供了开发者自定义的异常。
创建自定义的异常类,语法格式:
Class{自定因异常类名} extends Exception{
}
八:
1 需要根据软件的业务情况合理处理异常
-方法能够处理异常就处理,如果不能处理的异常,一定抛出到调用者。
-不能简单的抛弃异常
2 当抓住异常的时候,请务必输出:**调用栈-> e.printStackTrace()
3 throws: 不能乱用
九:
1.throw 关键字
当程序发生错误的而无法处理的时候,会抛出相对应的异常对象,除此之外,在某些时刻,你自己可以自行抛出一些异常,您就可以throw关键字,并生成指定的异常对象后抛出
例如:
Throw new ArithmeticException();
2.throws 关键字
程序中会声明许多方法(Method),这些方法中可以因某些错误而引发异常,但是您不希望自己处理,你就可以通过关键字:throws:来抛出异常
Public static void StringToDate(String str)throws ParseEXCEPTION{}
杂例异常:
import java.util.InputMismatchException; import java.util.Scanner; public class Demo14 { public static void main(String[] args) { Scanner in=new Scanner(System.in); /* * 使用try....catch处理可能发生的异常 * 如果没有异常 程序正常执行 如果有异常就跳到catch出继续处理执行 * 出现异常位置开始到以后的代码库不执行 */ while(true){ System.out.println("输入"); try{ int n=in.nextInt(); System.out.println(n); break; }catch(InputMismatchException e){ System.out.println("输入错误!!!"); in.nextLine();//吸收回车的字符 continue; } } } }