黑马程序员---(学习日记——异常)java中的异常机制
来源:互联网 发布:淘宝热卖网 编辑:程序博客网 时间:2024/05/21 11:14
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
1、异常类
系统,如内存溢出,链接错误等,一般 Java程序不
对这类运行错误进行处理,直接交给运 行时系统处理。
如,除数为0、数组下标越界等运行时产生的错误,
每种异常对象(描述运行错误的对象)
都对应着 一个异常类。
public class ExceptionArry{ public static void main (String args[]){ int index=0; int a_array[] = {1,2,3,4,5}; for(index=0;index<=5;index++) prt("a["+index+"]="+a_array[index]); prt("the end"); } public static void prt(String s){ System.out.println(s); }}
try{
可能发生错误的代码;
}
catch(Exception e){ 错误处理代码;
}
finally{ 最后要执行的代码;
}
{
可能产生异常的代码;
}
catch (exceptiontype1 e)
{异常e的处理语句}
catch (exceptiontype2 e)
{ 异常 e的处理语句}
catch (exceptiontype3 e)
{异常 e的处理语句 }
finally
{
最终的处理
}
public class CatchExceptDivideByZero { public static void main(String args[]) { try { inti,j = 0; i= 3 / j; //可能产生异常的语句置于try子句 } catch(ArithmeticException e) { //捕获算术异常 System.out.println("发生被零除异常....."); } finally { //无论是否发生异常都要执行该子句 System.out.println("\nEnd!"); System.out.println("\n程序继续执行,可正常结束!"); } }}
例:使用try-catch-finally捕获多个异常
public class Try2{ public static void main (String args[]) { int i=0; int a[] = {5,6,7,8}; for(i=0;i<5;i++) { try {System.out.print("a["+i+"]/"+i+"="+(a[i]/i)); } catch(ArrayIndexOutOfBoundsException e) { System.out.print("捕获数组下标越界异常!"); } catch(ArithmeticException e) { System.out.print("捕获算术异常!"); } catch(Exception e) { System.out.print("捕获"+e.getMessage()+"异常!"); } finally { System.out.println(" finally i="+i); } } System.out.println("继续!"); }}
注意:
常见的公用异常类
int a[]=null;
a[0]=0; //访问长度为0的数组,产生异常
String str=null;
System.out.println(str.length()); //产生异常
Object obj=new Object();
String str=(String) obj;
int a=new int[1];
a[0]=0;
a[1]=1;//下标越界,产生异常
inta[]=new int[-1]; //数组长度不能为负,产生异常
throw <异常对象>;
public class Ts{public void go(int i) throws Exception{if (i>0){throw new Exception("over boounds");}System.out.println("产生异常!");}public static void main(String[] args) {Ts x=newTs(); try{x.go(10);} catch(Exception e){ System.out.println(e.toString()); } }}
例:throws的使用。
public class Ex{ public static voidTest(String x,String y)throws NumberFormatException,ArithmeticException { inta=Integer.parseInt(x); int b=Integer.parseInt(y); System.out.println(a/b); } public static voidmain(String[] args) { try { Test(args[0],args[1]); } catch(NumberFormatException e) { System.out.println("数据转换异常"); } catch(ArithmeticException e) { System.out.println("捕获算术异常"); } finally { System.out.println("继续运行其他代码"); } } }
四、自定义异常
class OverflowException extendsException
{
public void printMsg()
{
System.out.println("exception:"+this.getMessage());
this.printStackTrace();
System.exit(0); //终止当前正在运行的 Java 虚拟机。参数用作状态码;根据惯例,非零的状态码表示异常终止
}
}
例 :public class Try7{ public void calc(byte k) throws OverflowException { byte y=1, i=1; System.out.print(k+"!="); for (i=1;i<=k;i++) { if(y>Byte.MAX_VALUE/i) throw new OverflowException(); else y = (byte)(y * i); } System.out.println(y); } public void run(byte k) { try { calc(k); } catch(OverflowException e) { e.printMsg(); } } public static void main (String args[]) { Try7 a = new Try7(); for (byte i=1;i<10;i++) a.run(i); }}
---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://www.itheima.com" target="blank">www.itheima.com</a>
- 黑马程序员---(学习日记——异常)java中的异常机制
- 黑马程序员-----java异常处理机制(学习日记)
- 黑马程序员java学习日记——异常和多线程
- 黑马程序员——学习日记10 java异常
- 黑马程序员——浅谈java中的异常机制
- 黑马程序员——Java中的异常机制总结
- 黑马程序员——学习笔记07.Java异常机制
- 黑马程序员——Java异常机制
- 黑马程序员——Java学习日记(六)异常、包
- 黑马程序员Java学习日记(3)异常,String,多线程
- 黑马程序员——学习日记3——异常机制
- 【黑马程序员】java中的异常处理机制
- 【黑马程序员】java中的---------------------异常机制
- 黑马程序员——java基础(异常机制、包)
- 黑马程序员——Java基础 异常机制 (复习)
- 黑马程序员——学习日记之内部类 异常
- Java日记(9)— 异常机制
- 黑马程序员学习日记(5)--异常与异常处理
- Minimum size subarray sum
- Python的filter, map, reduce, lambda
- Coursera-Crypto1 Week1
- 用++操作符完成其它操作符的转换
- 欢迎使用CSDN-markdown编辑器
- 黑马程序员---(学习日记——异常)java中的异常机制
- 《C Primer Plus(第五版)中文版》第10章第1至13题
- Python的itertools和迭代器
- 让 Chrome 变成 Notepad
- UIScrollView 原理详解
- 5.9
- 深入理解iOS开发中的UIScrollView
- P122.14
- ImportError: libffi.so.6: cannot open shared object file: No such file or directory