package JavaBasis.chapter7;publicclassIndexOutExp{publicstaticvoidmain(String[] args){
String friends[]={"lisa","biby","mike"};for(int i=0;i<5;i++){
System.out.println(friends[i]);}
System.out.println("end");}}
lisa
biby
mike
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:3
at JavaBasis.chapter7.IndexOutExp.main(IndexOutExp.java:8)
package JavaBasis.chapter7;publicclassNullRef{int i=1;publicstaticvoidmain(String[] args){
NullRef t=newNullRef();
System.out.println(t.i);
t=null;
System.out.println(t.i);}}1
Exception in thread "main" java.lang.NullPointerException
at JavaBasis.chapter7.NullRef.main(NullRef.java:9)
package JavaBasis.chapter7;publicclassDivideZero{int x;//默认值0publicstaticvoidmain(String[] args){int y;
DivideZero c=newDivideZero();
y=3/c.x;
System.out.println("end");}}
Exception in thread "main" java.lang.ArithmeticException:/ by zero
at JavaBasis.chapter7.DivideZero.main(DivideZero.java:8)
PS D:\VS_Code_Java>
package JavaBasis.chapter7;import java.util.Date;publicclassOrder{publicstaticvoidmain(String[] args){
Object obj=newDate();
Order order;
order=(Order)obj;//类型转化异常
System.out.println(order);}}
Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to JavaBasis.chapter7.Order
at JavaBasis.chapter7.Order.main(Order.java:9)
package JavaBasis.chapter7;publicclassIndexOutExp{publicstaticvoidmain(String[] args){
String friends[]={"lisa","biby","mike"};try{for(int i=0;i<5;i++){
System.out.println(friends[i]);}}catch(ArrayIndexOutOfBoundsException e){
System.out.println("index error");}
System.out.println("end");}}
lisa
biby
mike
index error
end
package JavaBasis.chapter7;publicclassDivideZero{int x;//默认值0publicstaticvoidmain(String[] args){int y;
DivideZero c=newDivideZero();try{
y=3/c.x;}catch(Exception e){
System.out.println("divided by zero error");}
System.out.println("end");}}
divided by zero error
end
package JavaBasis.chapter7;import java.io.FileInputStream;publicclassIOExp{publicstaticvoidmain(String[] args){try{
FileInputStream in=newFileInputStream("D://VS_Code_Java//JavaBasis//chapter7//a.txt");int b;
b=in.read();while(b!=-1){
System.out.println((char)b);
b=in.read();}
in.close();}catch(Exception e){
System.out.println(e);}finally{
System.out.println("read end");}}}
h
e
l
l
o
w
o
r
l
d
read end