异常的分类:
按错误级别:Error:是程序中无法处理的错误,表示运行应用程序中出现了严重的错误。此 类 错误发生时,JVM将终止线程应用不应该去处理此类错误。通常有Virtual MachineError(虚拟机运行错误)、NoClassDefFoundError(类定义错误)等。比如说当jvm耗完可用内存 将出现OutOfMemoryError。
Exception:程序本身可以捕获并且可以处理的异常
按处理时机:
运行时异常:RuntimeException及子类的异常都是,在程序运行期间产生的逻辑错误异常,此类异常属于不可查异常,一般是由程序逻辑错误引起的,在程序中可以选择捕获处理,也可以不处理。
public class ExceptionShowDemo {
public static void main(String[] args) {
int[] arr = new int[1024*1024*1024];//OutOfMemoryError:jvm耗完可用内存
//1kb = 1024 byte 1m = 1024kb 1G = 1024mb
System.out.println(arr);
}
}
public class ExceptionShowDemo {
public static void main(String[] args) {
int a = Integer.parseInt(null);//NumberFormatException:空指针异常
System.out.println(a);
}
}
public class ExceptionShowDemo {
public static void main(String[] args) {
int du = du(4, 0);//ArithmeticException:除数不能为0异常
System.out.println(du);
}
static int du(int a,int b){
return a/b;
}
}
异常的体系:
Throwable
Error:jvm奔溃或系统资源耗尽等,需要调整系统配置设置等参数解决,而非修改代码解决
Exception
RunTimeException(运行时异常)及其子类异常
IOException (编译时异常)
异常的处理:
抛出异常throw:
throw用在方法内,用来抛出一个异常对象,将这个异常对象传递到调用者处,并结 束当前方法的执行。
声明抛出异常throws:
运用于方法声明之上,用于表示当前方法不处理异常,而是提醒该方法的调用者来处理异常
public class ExceptionShowDemo {
public static void main(String[] args) throws ArithmeticException{
System.out.println(du(6, 0));//ArithmeticException: 整除法中,除数不能为0!
}
static int du(int a,int b)throws ArithmeticException{
if (b == 0) {
throw new ArithmeticException("整除法中,除数不能为0!");
}
return a/b;
}
}
自定义异常类:
通过继承Exception成为其子类(编译时异常)
通过继承RuntimeException成为其子类(运行时异常)
意义:可以自己来封装异常信息到自己定义的异常对象中,按照自己的规则处理异常数据。
public class MException extends RuntimeException/*Exception*/ {
public String ms;
public String getMs() {
return ms;
}
public void setMs(String ms) {
this.ms = ms;
}
public MException(String ms) {
super(ms);
this.ms = ms;
}
}
public class ExceptionD {
public int getva(int[] arr,int index) throws MException{
if (index < 0) {
throw new MException("索引不能为负数:"+index);
}
if (index >= arr.length) {
throw new MException("索引超过了数组的长度:"+index);
}
if (arr == null) {
throw new MException("数组没有指向实体对象"+null);
}
return arr[index];
}
public static void main(String[] args) {
ExceptionD ea = new ExceptionD();
try {
int[] a = new int[]{1,2,34,5};
ea.getva(a,4);
} catch (MException e) {
System.out.println(e.getMs());
}finally {
System.out.println("女孩你好");
}
}
}
异常注意事项
1、捕获的异常有多个,多个异常之间存在继承关系,则子类异常要放在捕获的前面,父类异常放后面捕获
2、直接使用父级异常代替子级异常进行捕获是可以的
3、 异常捕获处理后,后续的代码可以继续执行
4、 子类的异常不能超过父类中的异常
处理原则:能在自己范围内处理的异常,捕获异常并合理处理,实在处理不了的异常将异常向调用者声明并抛出。
public class FengZhuang {
public static void main(String[] args) {
Integer In = new Integer(5);
int a = In + 14; //自动拆箱
System.out.println(a);
Integer b = In +5; //自动装箱
System.out.println(b);
}
}
Integer进制转换:
public class FengZhuang {
public static void main(String[] args) {
Integer In = new Integer(5);
int a = In + 14; //自动拆箱
System.out.println(a);
Integer b = In +5; //自动装箱
System.out.println(b);
System.out.println(Integer.toHexString(10));//将整数i转换成十六进制字符串形式
System.out.println(Integer.toBinaryString(12));//将整数i转换成二进制字符串形式
System.out.println(Integer.toString(10,16));//将值转换成十进制字符串形式
System.out.println(Integer.parseInt("12", 9));//第一个参数为要解析的字符串,第二个参数是要转换的进制,范围为(0-32)
}
}
不乱于心,不困于情,不念过去,不畏将来。
作者:普通人