马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在Java中我们把步伐实验过程中不正常的举动称为非常。
一 常见的非常
1.算数非常
- public class Test {
- public static void main(String[] args) {
- int a=10;
- System.out.println(a/0);
- }
- }
复制代码
2.数组越界非常
- public class Test {
- public static void main(String[] args) {
- char[] array={1,2,3};
- System.out.println(array[5]);
- }
复制代码
3.空指针非常
- public class Test {
- public static void main(String[] args) {
- char[] array=null;
- System.out.println(array.length);
- }
复制代码
二 非常的体系结构
1.throwable是非常类的顶层类,是全部非常类的父类 ,Error 和 Exception
2.Error:指的是Java虚拟机无法解决的严重问题,好比:JVM的内部错误、资源耗尽等,典范代表: StackOverflowError和OutOfMemoryError,一旦发生回力乏术。
3.Exception:指的是步伐员可以通过修改代码处理的非常。
三 非常的分类
1.编译时非常
一般发生在编译期间,也称为受查抄非常(Checked Exception)
- public class Test {
- public static void main(String[] args) {
- Person person=new Person();
- Person person1=(Person)person.clone();
- }
复制代码
2.运行时非常
在运行之后才会显示的非常
四 非常的处理
在Java中,非常时根据一些关键词来处理的,try,catch,finally,throw,throws。这些关键词提供了结构化的方法来处理运行时的非常。
1.try-catch结构
try 关键字用于包罗可能会抛出非常的代码块,catch 用于捕获和处理非常。
根本结构
- try {
- // 可能会抛出异常的代码
- } catch (ExceptionType1 e1) {
- // 处理 ExceptionType1 异常
- } catch (ExceptionType2 e2) {
- // 处理 ExceptionType2 异常
- } finally {
- // 无论是否抛出异常,finally 代码块都会执行
- }
复制代码 示例:
- public class Test {
- public static void function(int a)throws CloneNotSupportedException{
- if(a==10){
- throw new CloneNotSupportedException();
- }
- }
- public static void main(String[] args) {
- try {
- function(10);
- System.out.println("符合了异常条件");
- }catch(CloneNotSupportedException e){
- System.out.println("捕获了异常,处理异常");
- }finally {
- System.out.println("lalalalala");
- }
- System.out.println("没有问题,程序正常运行");
- }
复制代码 注:如果有非常,则抛出非常的代码块,非常后面的代码无法运行。
finally 代码块无论何时都会实验。
2.多重捕获
如果代码匹配哪个非常,则会进行哪个catch块进行处理
- public static void main(String[] args) {
- try {
- int[] arr=new int[3];
- arr[5]=8;
- }catch (ArithmeticException e){
- System.out.println("算数异常"+e.getMessage());
- }catch (ArrayIndexOutOfBoundsException e){
- System.out.println("数组越界异常"+e.getMessage());
- }finally {
- System.out.println("结束");
- }
- }
复制代码 如果没有匹配到合适的catch,代码则会继续向上实验,直到合适的地方捕获到非常,或者直接崩溃。
注:
在Java 7之后答应一个代码块实验多个非常
- public static void main(String[] args){
- try {
- int[] arr = new int[3];
- arr[5] = 8;
- } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
- System.out.println("算数异常" + e.getMessage() + "数组越界异常" + e.getMessage());
- } finally {
- System.out.println("结束");
- }
- }
复制代码
3.finally块
finally块的代码无论是否抛出非常,都会被实验,通常用于清理资源,如关闭文件、释放数据库毗连等。如果try-catch中有return,代码块也会被实验。
- public static void main(String[] args) {
- try {
- int[] arr=new int[3];
- arr[5]=8;
- }catch (ArithmeticException e){
- System.out.println("算数异常"+e.getMessage());
- return;
- }catch (ArrayIndexOutOfBoundsException e){
- System.out.println("数组越界异常"+e.getMessage());
- return;
- }finally {
- System.out.println("结束");
- }
- }
复制代码
4. throw
throw关键词用于手动抛出非常,可以被用于任何地方
- public static void sex(String sex){
- if(sex=="男"){
- throw new ArithmeticException("不能进入女子队");
- }else {
- System.out.println("可以进入女子队伍");
- }
- }
- public static void main(String[] args) {
- sex("男");
- }
复制代码
满意性别为男性时,步伐手动抛出 ArithmeticException。
5.throws
throws关键词用于声明,体现该方法可能抛出的哪些非常,被标记的非常要么被捕捉,要么在方法调用的地方继续抛出。
- public static void ReadFile() throws IOException {
- FileReader fileReader=new FileReader("text.c");
- }
- public static void main(String[] args) throws IOException{
- ReadFile();
- }
复制代码 6.自界说非常
Java 答应你界说自己的非常类,通常通过继承 Exception 或 RuntimeException 来实现。自界说非常可以帮助你根据业务逻辑界说特殊的错误环境。
如果不用自界说非常的方法写一个登录途径
- class login{
- private String name;
- private String num;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getNum() {
- return num;
- }
- public void setNum(String num) {
- this.num = num;
- }
- public static void login(String name,String num ){
- Scanner scanner=new Scanner(System.in);
- if(!name.equals(name)){
- System.out.println("姓名输入错误");
- return;
- }else if (!num.equals(num)){
- System.out.println("账号输入错误");
- return;
- }
- System.out.println("登陆成功");
- }
- public static void main(String[] args) {
- login("zhangsan","123456");
- }
- }
复制代码
使用自界说非常
- class NameException extends Exception {
- public NameException(String message) {
- super(message);
- }
- }
- class NumException extends Exception {
- public NumException(String message) {
- super(message);
- }
- }
- class login{
- private String name;
- private String num;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getNum() {
- return num;
- }
- public void setNum(String num) {
- this.num = num;
- }
- public static void login(String name,String num ) throws NameException,NumException{
- Scanner scanner=new Scanner(System.in);
- if(!name.equals(name)) {
- throw new NameException("姓名输入错误");
- }else if (!num.equals(num)){
- throw new NumException("账号输入错误");
- }
- System.out.println("登陆成功");
- }
- public static void main(String[] args) {
- try {
- login("zhangsan","123456");
- }catch (NameException e){
- e.printStackTrace();
- }catch (NumException e){
- e.printStackTrace();
- }
- }
- }
复制代码 7. 总结:
- try-catch-finally 是非常处理的核心结构,try 用于包罗可能抛出非常的代码,catch 用于捕获和处理非常,finally 块用于清理资源。
- throw 用于手动抛出非常,而 throws 用于声明方法可能抛出的非常。
- 处理非常时,建议只捕获你能处理的非常,避免捕获过于笼统的非常,如直接捕获 Exception,如许会掩饰潜在的编程错误。
- finally 块应尽可能用于释放资源,避免资源泄露问题。
- 通过自界说非常,可以根据业务需求界说特定的非常来增强代码的可读性和可维护性。
希望能对各人有所帮助!!!!!!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |