异常
Throwable
Eorre Exception
RunTimeException 除RunTimeException
运行时异常
throws
写在方法定义处 自己不处理,交给方法调用者处理,main方法不处理会交给虚拟机
thow 在方法内,跟的是异常对象名 后面跟运行异常,不需要做额外处理
后面跟的编译异常,需要在声明处通过throws将对应 的异常进行声明
JVM
打印异常 终止运行
try....catch 自己处理异常
try{
有可能出现先问题的代码
}catch(异常的名字){
如果出现了这样的异常我们怎末操作,不会用JVM默认的处理方式了
}
多个异常提供多个catch 提供一个兜底异常 Excepion 父类异常最后
异常对象.getMessage() 将异常消息转化成字符串输出在控制台
异常对象.toString() 打印异常类型+上面的后续
异常对象.printStaticTrace()比前面的多了异常代码的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.yang.API.exception; public class MyException { public static void main(String[] args) throws Exception { int score=- 1 ; method(score); } public static void method( int score) throws Exception{ if (score< 0 ||score> 100 ){ throw new Exception( "输入正确的数据" ); } else { System.out.println( "你的分数是" +score); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.yang.API.runtime_excption; public class MyRunTimeExcption { public static void main(String[] args) { int score=- 1 ; try { method(score); } catch (RuntimeException e){ System.out.println( "请你输入正常的分数" ); } } public static void method( int score) { if (score< 0 ||score> 100 ){ throw new RuntimeException( "输入错误" ); } else { System.out.println( "你考了" +score); } } } |
1 2 3 4 5 6 7 | 自定义 异常类(见名知意) extends RunTimeException{ <br> 无参构造<br><br> public 异常类(String message){ super (message) } } |
MyTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.yang.API.MyEceptionClass; import java.util.Scanner; public class MyTest { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); Student student= new Student(); System.out.println( "请输入你的年龄" ); while ( true ){ String name=scanner.next(); student.setName(name); System.out.println( "姓名是" +student.getName()); try { System.out.println( "请输入你的分数" ); int age=Integer.parseInt(scanner.next()); student.setAge(age); break ; } catch (NumberFormatException n){ System.out.println( "请输入一个整数" ); } catch (AgeOutOfBoundRunTimeException a){ System.out.println( "你输入的年龄超出了范围" ); } } System.out.println( "你很聪明的输入你的分数年龄" +student.getAge()); } } |
Student
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | package com.yang.API.MyEceptionClass; public class Student { String name; int age; public Student(String name, int age) { this .name = name; this .age = age; } public Student() { } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { if (age< 0 ||age> 18 ){ throw new AgeOutOfBoundRunTimeException( "你输入的分数范围不对" ); } else { this .age = age; } } @Override public String toString() { return "Student{" + "name='" + name + '\ '' + ", age=" + age + '}' ; } } |
AgeOutOfBoundRunTimeException
1 2 3 4 5 6 7 8 9 10 | package com.yang.API.MyEceptionClass; public class AgeOutOfBoundRunTimeException extends RuntimeException{ public AgeOutOfBoundRunTimeException() { } public AgeOutOfBoundRunTimeException(String message) { super (message); } } |