java学习总结——第十一天

异常:

程序在运行过程中遇到的错误称为异常。

所有异常的父类是:java.lang.Throwable,他有两个重要的分支:ErrorException

 

                    Throwable

                     /                \

                /                          \

        Error                             Exception

                                                \            

                                  RuntimeException

 

 

Error :虚拟机引发的较严重的错误

Exception:可以通过程序进行处理的异常

 

java内置了许多表示异常原因的异常类:

NumberFormatException                 转换数字异常

NullPointerException                    空指针异常

ArithmeticException   算数异常

ArrayIndeOutOfBoundsException   数组下标越界

IllegalArgumentException   方法参数不正确

ClassCastException   类型不匹配

OutofMemoryError                       内存溢出

NoClassDefFoundError                    找不到类

 

 

异常抛出:

try {               //嵌套

// byte[] b = new byte[1024 * 1024 * 200];

try {

 

byte[] b = new byte[1024 * 1024 * 200];

} catch (Exception e) {

// TODO: handle exception

}

//System.out.println("Hello");

} catch (Throwable e) {

if (e instanceof java.lang.OutOfMemoryError) {

System.out.println("内存溢出");

} else {

 

System.out.println("内存不够!2");

}

//System.out.println("Hello");

}finally{

//最终执行块,一般用于资源的关闭

System.out.println("Hello");

}

 

自定义的异常:

public void setName(String name) throws RuntimeException {      //声明异常

 

if (name.length() <= 0 || name.length() >= 4) {

throw new RuntimeException();               //抛出异常

}

 

this.name = name;

 

}

 

public static void main(String[] args) {

 

try {

 

new Test2().setName("1234");

} catch (Exception e) {

System.out.println("名字长度超出或者是过短");

}

 

 

 

 

抛出异常综合例题:

public class Test1 {

public static void method1() throws NullPointerException {

try {

System.out.println("0");

throw new NullPointerException();

} finally {

System.out.println("1");

}

}

 

public static void method2() {

System.out.println("2");

method1();

System.out.println("3");

}

public static void method3()throws NullPointerException{

try{

System.out.println("4");

method2();

System.out.println("5");

}catch(NullPointerException e){

System.out.println("6");

throw e;

}finally{

System.out.println("7");

}

}

public static void main(String[] args) {

method3();

}

}

运行结果:4 2 0 1 6 7(一字一行)。

***抛出异常后先执行finally块然后去处理异常。

异常后的语句不再执行(除fianlly块)。

 

运行时错误不要求抛异常

 

Getmessage():得到异常信息

printStackTrace():将此throwable及其追踪输出至标准错误流。

 

 

 

 

基本数据类型的封装类

Int           Integer 

float        Float

double        Double

char       Character

 

long          Long

short      Short

byte          Byte

boolean       Boolean

 

Integer.parseInt(“123”);            将“123”字符串转换为10进制的整数

Interger.parseInt(“123”,16)          将“123”字符串转换为16进制的整数

 

Integer.toBinaryString(15);          整数转换为2进制数

Integer.toHexString(15); 整数转换为16进制数

Integer.toOctalString(15); 整数转换为8进制数

 

Integer.valueof(100);                整数类型的数转换为整数类型的字符串

Integer.valueof(“123ABC”,16);        16进制的字符串转换为10进制的整数型的字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值