JAVA 异常
目录[ Hide ]
JVM默认是如何处理异常的
try…catch的方式处理异常1
package com.heima.exception;
public class Demo2_exception {
//try:用来检测异常
//catch:用来捕获异常
//finally:释放资源
public static void main(String[] args) {
Demo d = new Demo();
try{
int x = d.dic(10, 0);
System.out.println(x);
}catch(ArithmeticException a){} //ArithmeticException a = new ArithmeticException();
System.out.println("除数出错了 不能为0");
}
}
class Demo{
//除法
public int dic(int a, int b){
return a / b;
}
}
public class Demo2_exception {
//try:用来检测异常
//catch:用来捕获异常
//finally:释放资源
public static void main(String[] args) {
Demo d = new Demo();
try{
int x = d.dic(10, 0);
System.out.println(x);
}catch(ArithmeticException a){} //ArithmeticException a = new ArithmeticException();
System.out.println("除数出错了 不能为0");
}
}
class Demo{
//除法
public int dic(int a, int b){
return a / b;
}
}
try…catch的方式处理异常2
package com.heima.exception;
public class Demo3_exception {
//try catch处理多个异常
/*安卓,客户端开发,如何处理异常?try{}catch(Exception e){}
* ee 服务端开发,一般都是底层开发,从底层向上抛
*
* try 后面如果跟多个catch,小的异常放前面,大的异常放后面,根据多态的原理,如果大的放前面,
* 就会将所有的子类对象接收,后面的catch就没有意义了
* */
public static void main(String[] args) {
// demo1();
int a = 10 ;
int b = 0 ;
int[]arr = {11,22,33,44,55};
//JDK7如何处理异常
try {
//System.out.println(a / b);
//System.out.println(arr[10]);
arr = null;
System.out.println(arr[0]);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
// TODO: handle exception
System.out.println("索引越界了");
}
}
private static void demo1() {
int a = 10 ;
int b = 0 ;
int[]arr = {11,22,33,44,55};
try {
//System.out.println(a / b);
//System.out.println(arr[10]);
arr = null;
System.out.println(arr[0]);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println("索引越界了");
}catch (NullPointerException e) {
// TODO: handle exception
System.out.println("空指针异常");
}catch (Exception e) {
// TODO: handle exception
System.out.println("出错了");
}
}
}
public class Demo3_exception {
//try catch处理多个异常
/*安卓,客户端开发,如何处理异常?try{}catch(Exception e){}
* ee 服务端开发,一般都是底层开发,从底层向上抛
*
* try 后面如果跟多个catch,小的异常放前面,大的异常放后面,根据多态的原理,如果大的放前面,
* 就会将所有的子类对象接收,后面的catch就没有意义了
* */
public static void main(String[] args) {
// demo1();
int a = 10 ;
int b = 0 ;
int[]arr = {11,22,33,44,55};
//JDK7如何处理异常
try {
//System.out.println(a / b);
//System.out.println(arr[10]);
arr = null;
System.out.println(arr[0]);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
// TODO: handle exception
System.out.println("索引越界了");
}
}
private static void demo1() {
int a = 10 ;
int b = 0 ;
int[]arr = {11,22,33,44,55};
try {
//System.out.println(a / b);
//System.out.println(arr[10]);
arr = null;
System.out.println(arr[0]);
} catch (ArithmeticException e) {
System.out.println("除数不能为0");
}catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
System.out.println("索引越界了");
}catch (NullPointerException e) {
// TODO: handle exception
System.out.println("空指针异常");
}catch (Exception e) {
// TODO: handle exception
System.out.println("出错了");
}
}
}
编译期异常和运行期异常的区别
package com.heima.exception;
import java.io.FileInputStream;
public class Demo4_exception {
public static void main(String[] args) {
try{
//编译时异常,不使用异常无法成功编译
FileInputStream fis = new FileInputStream("xxx.txt");
}catch(Exception e){
}
}
}
import java.io.FileInputStream;
public class Demo4_exception {
public static void main(String[] args) {
try{
//编译时异常,不使用异常无法成功编译
FileInputStream fis = new FileInputStream("xxx.txt");
}catch(Exception e){
}
}
}
Throwable的几个常见方法
package com.heima.exception;
public class Demo5_Throwable {
public static void main(String[] args) {
try {
System.out.println(1/0);
} catch (Exception e) { //Exception e = new ArithmeticException("/ by zero");
/*System.out.println(e.getMessage());//获取异常信息
System.out.println(e.toString());
System.out.println(e);//调用toString方法打印异常类名和异常信息
*/
e.printStackTrace(); //jvm默认使用这种方法处理异常
}
}
}
public class Demo5_Throwable {
public static void main(String[] args) {
try {
System.out.println(1/0);
} catch (Exception e) { //Exception e = new ArithmeticException("/ by zero");
/*System.out.println(e.getMessage());//获取异常信息
System.out.println(e.toString());
System.out.println(e);//调用toString方法打印异常类名和异常信息
*/
e.printStackTrace(); //jvm默认使用这种方法处理异常
}
}
}
throws的方式处理异常
package com.heima.exception;
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出可以处理可以不处理
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age>0 && age<=150){
this.age = age;}
else{throw new Exception("年龄非法");}
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出可以处理可以不处理
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws Exception {
if(age>0 && age<=150){
this.age = age;}
else{throw new Exception("年龄非法");}
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
throw的概述以及和throws的区别
package com.heima.exception;
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出可以处理可以不处理
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//异常类名可以有多个
public void setAge(int age) throws Exception,RuntimeException {
if(age>0 && age<=150){
this.age = age;}
else{
Exception e = new Exception("年龄非法");
//异常引用只能有一个
throw e;
}
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出可以处理可以不处理
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//异常类名可以有多个
public void setAge(int age) throws Exception,RuntimeException {
if(age>0 && age<=150){
this.age = age;}
else{
Exception e = new Exception("年龄非法");
//异常引用只能有一个
throw e;
}
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
finally关键字的特点及作用
package com.heima.exception;
public class Demo7_Finally {
//return语句相当于非法的最后一口气,在他将死之前会看看也没用finally,会帮其完成遗愿,如果有就将finally执行后返回
public static void main(String[] args) {
try {
System.out.println(10/0);
//System.out.println(1/0);
} catch (Exception e) {
System.out.println("除数为0了");
System.exit(0);//退出java虚拟机
return;
} finally {
System.out.println("是否执行");
}
}
}
public class Demo7_Finally {
//return语句相当于非法的最后一口气,在他将死之前会看看也没用finally,会帮其完成遗愿,如果有就将finally执行后返回
public static void main(String[] args) {
try {
System.out.println(10/0);
//System.out.println(1/0);
} catch (Exception e) {
System.out.println("除数为0了");
System.exit(0);//退出java虚拟机
return;
} finally {
System.out.println("是否执行");
}
}
}
finally关键字的面试题
由于finally里不能写返回语句,所以返回的是return之前的数据
自定义异常概述和基本使用
package com.heima.exception;
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出可以处理可以不处理
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//异常类名可以有多个
public void setAge(int age) throws AgeOutOfBoundsException {
if(age>0 && age<=150){
this.age = age;}
else{
throw new AgeOutOfBoundsException("年龄非法");
}
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出可以处理可以不处理
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setAge(-17);
System.out.println(p.getAge());
}
}
class Person{
private String name;
private int age;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//异常类名可以有多个
public void setAge(int age) throws AgeOutOfBoundsException {
if(age>0 && age<=150){
this.age = age;}
else{
throw new AgeOutOfBoundsException("年龄非法");
}
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
异常练习
package com.heima.test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Test2 {
/*
* 1.创建键盘录入对象
* 2.将键盘录入的结果存储在String类型的字符串中,存储int类型中如果又不符合条件的直接报错无法进行后续判断
* 3.将键盘录入的结果转换成int类型的数据,是正确的还是错误的
* 4.正确的直接转换
* 5.错误的要进行对应的判断
* */
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
while(true){
String line = sc.nextLine(); //将键盘录入的结果存储在line中
try {
int num = Integer.parseInt(line);//将字符串转换为整数
System.out.println(Integer.toBinaryString(num));//将整数转换为二进制
break; //正确跳出循环
} catch (Exception e) {
try {
new BigInteger(line);
System.out.println("录入错误,录入的数过大,请重新输入");
} catch (Exception e2) {
try {
new BigDecimal(line); //alt+shift+z(try catch 快捷键)
System.out.println("录入错误,您录入的是一个小数,请重新输入");
} catch (Exception e3) {
System.out.println("录入错误,您录入的是非法字符,请重新输入");
}
}
}
}
}
}
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Test2 {
/*
* 1.创建键盘录入对象
* 2.将键盘录入的结果存储在String类型的字符串中,存储int类型中如果又不符合条件的直接报错无法进行后续判断
* 3.将键盘录入的结果转换成int类型的数据,是正确的还是错误的
* 4.正确的直接转换
* 5.错误的要进行对应的判断
* */
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
while(true){
String line = sc.nextLine(); //将键盘录入的结果存储在line中
try {
int num = Integer.parseInt(line);//将字符串转换为整数
System.out.println(Integer.toBinaryString(num));//将整数转换为二进制
break; //正确跳出循环
} catch (Exception e) {
try {
new BigInteger(line);
System.out.println("录入错误,录入的数过大,请重新输入");
} catch (Exception e2) {
try {
new BigDecimal(line); //alt+shift+z(try catch 快捷键)
System.out.println("录入错误,您录入的是一个小数,请重新输入");
} catch (Exception e3) {
System.out.println("录入错误,您录入的是非法字符,请重新输入");
}
}
}
}
}
}