ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【Java】异常处理

2021-05-30 02:02:48  阅读:155  来源: 互联网

标签:Java string 处理 代码 standard input integer 异常


在传统的代码中,你必须包含一些代码去处理程序错误,但是这样会导致程序很难阅读。

假设有以下流程:

int readInteger() {
    Read a string from the standard input;
    Convert the string to an integer value;
    Return the integer value;
}

以上代码只是实现了流程,但是忽视了程序中可能出现的错误:

  • 输入的字符串可能无法从标准输入里读取,比如输入的可能是一个损坏的文件;
  • 输入的字符不包含数字,比如输入的是"2r"而不是"25"

为了使得代码更具有鲁棒性,你必须加入一些处理潜在错误的代码:

int readInteger() {
    while (true) {
        read a string from the standard input;
        if (read from the standard input fails) {
            handle standard input error;
        } else {
            convert the string to an integer value;
            if (the string does not cantain an integer) {
                handle invalid number fromat error;
            } else {
                return the integer value;
            }
        }
    }
}

但是这样写的代码阅读性很差,业务代码和异常处理代码互相夹在在一起,代码难以阅读。引入的异常处理机制可以将业务代码和异常处理代码分开,将异常集中起来处理。因此,上述代码可以改写为:

int readInteger() {
    while (true) {
        try {
            read a string from the standard input;
            convert the string to an integer value;
            return the integer value;
        } catch (read from the standard input failed) {
            handle the standard input error;
        } catch (the string does not contain an integer) {
            handle invalid number format error;
        }
    }
}

可以看到业务代码被集中到try代码块里,而对异常的抓取和处理则集中到了catch代码块里。

主要需要掌握三种类型的异常:

  • 检查性异常:因为用户的错误导致的异常,程序员无法预见。比如用户打开一个不存在的文件时,一个异常就发生了,此种异常在编码时不能被简单忽略。
  • 运行时异常:运行时异常是可能被避免的异常。在编译时可以被忽略。
  • 错误:错误不是异常,而是脱离程序员控制的问题。错误在代码中通常被忽略。例如,当栈溢出时,一个错误就发生了。它们在编译时也检查不到。

标签:Java,string,处理,代码,standard,input,integer,异常
来源: https://www.cnblogs.com/wildcardo/p/14826921.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有