异常

基本介绍

  • 什么是异常?

    • 在实际工作中,遇到的情况不可能是非常完美。比如:你写的某个模块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对
    • 软件程序在运行过程中,非常可能遇到刚刚提到的异常问题(Exception),对于这些异常,怎么让我们写的程序做出合理的处理,不至于让程序奔溃
    • 异常指程序运行中出现的不期而至的的各种情况,如:文件找不到,网路连接失败,非法参数等
    • 异常发生在程序运行期间,他影响了正常的程序流程
    • 异常分为三类:检查性异常;运行时异常;错误error;
  • 异常处理机制
    • 抛出异常
    • 捕获异常
    • 异常处理的五个关键字:try,catch,finally,throw,throws

运用实例

public class ChuLi {
public static void main(String[] args) {
//Ctrl+Alt+t快捷键可以try catch
try {
new ChuLi().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
} /*
int a=1;
int b=0;
//假设要捕获多个异常,记得从小到大!
try {//try监控区域
System.out.println(a/b);
}catch (Error e){//catch(想要捕获的异常类型)捕获异常
System.out.println("Error");
}catch (Exception e){
System.out.println("Exception");
}catch (Throwable t){
System.out.println("Throwable");
}finally {//处理善后工作(也可不用finally,一般用在关闭资源,io等)
System.out.println("finally");
} */
} //假如在这个方法,处理不了这个异常,方法上抛出异常
public void test(int a,int b)throws ArithmeticException{
if (b==0){//throw throws
throw new ArithmeticException();//主动抛出异常情况,一般在方法中使用
}
}
}

异常(Java)的更多相关文章

  1. 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method

    异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...

  2. android异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused

    android手机做下载文件时,报了如下异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused 模拟器 ...

  3. socket编程报异常java.io.EOFException

    一个客户端连接服务器的小程序,服务器端可以正常读取客户端发来的数据 但是当客户端关闭时,服务端也关闭了,并且抛出如下的异常: java.io.EOFException at java.io.DataI ...

  4. 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity

    异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...

  5. spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet

    spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet   情况 ...

  6. Storm 执行异常 java.lang.RuntimeException: java.nio.channels.UnresolvedAddressException 问题解决

    最近写的 binlog2kafka storm job 上线在一个新的集群环境中(storm 0.9.0.1, kafka 0.8), storm job 运行时报出如下异常: java.lang.R ...

  7. maven(多个模块)项目 部署 开发环境 问题处理历程【异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE】

    maven(多个模块)项目 部署 开发环境 问题处理历程[异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE] 201 ...

  8. AES加解密异常java.security.InvalidKeyException: Illegal key size

    AES加解密异常 Java后台AES解密,抛出异常如下:java.security.InvalidKeyException: Illegal key size Illegal key size or ...

  9. 异常-java.util.concurrent.TimeoutException: Futures timed out after [100000 milliseconds]

    1 详细异常 java.util.concurrent.TimeoutException: Futures timed out after [100000 milliseconds] at scala ...

  10. 项目启动异常,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo ...

随机推荐

  1. Array方法学习总结

    Array 对象支持在 单个变量名下存储多个元素. Array方法: 在遍历多个元素的方法中,下面的方法在访问索引之前执行in检查,并且不将空槽与undefined合并:concat() 返回一个新数 ...

  2. 并发多线程学习(五)Java线程的状态及主要转化方法

    1 操作系统中的线程状态转换 首先我们来看看操作系统中的线程状态转换. 在现在的操作系统中,线程是被视为轻量级进程的,所以操作系统线程的状态其实和操作系统进程的状态是一致的. 操作系统线程主要有以下三 ...

  3. 13-之容器资源需求、资源限制及Metric-server(Heapster)

    目录 容器资源需求.资源限制及Heapster Heapster 资源指标API及自定义指标API k8s-promtheus监控部署 node-exporter prometheus kube-st ...

  4. Appkiz.Base、Appkiz.Base.Languages

    环境: ILSpy version 4.0.0.4319-beta2 选择 C#6.0 Visual Studio 2015 直接保存代码,直接用Visual Studio 2015打开.csprj文 ...

  5. hyperfine spectra

    !Hyperfine spectradefine int n xlet xlet name spect-'x'!file in 'name'.basfindget 3set mod x aset un ...

  6. vm虚拟机和主机之间互传文件

    u盘大家都有吧,用u盘吧,超方便! vmtools 从主机传文件到虚拟机 可以通过之间复制粘贴/拖拽 或者共享文件夹的方式 从虚拟机传文件到主机,查到了说说是要在虚拟机里面改一个什么映射设置,改完之后 ...

  7. Linux profile、bashrc、bash_profile

    一.profile 文件 1.profile 文件的作用 profile(/etc/profile),用于设置系统级的环境变量和启动程序,在这个文件下配置会对所有用户生效.当用户登录(login)时, ...

  8. PNAS:微生物组分析揭示人类皮肤的独特性

    https://blog.csdn.net/woodcorpse/article/details/87989663

  9. Datax初使用

    为了掌握大数据采集技术,自学习了datax的使用 简介: DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.HDFS.Hive.OceanBase.H ...

  10. VUE学习-编译到浏览器

    vue文件时怎样编译运行到浏览器的 http://www.manongjc.com/article/21832.html 通过vue-loader,解析.vue文件,在webpack解析,拆解vue组 ...