异常(Java)
异常
基本介绍
- 什么是异常?
- 在实际工作中,遇到的情况不可能是非常完美。比如:你写的某个模块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在或者文件格式不对
- 软件程序在运行过程中,非常可能遇到刚刚提到的异常问题(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)的更多相关文章
- 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method
异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...
- android异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
android手机做下载文件时,报了如下异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused 模拟器 ...
- socket编程报异常java.io.EOFException
一个客户端连接服务器的小程序,服务器端可以正常读取客户端发来的数据 但是当客户端关闭时,服务端也关闭了,并且抛出如下的异常: java.io.EOFException at java.io.DataI ...
- 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity
异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...
- spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet
spring异常 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderServlet 情况 ...
- Storm 执行异常 java.lang.RuntimeException: java.nio.channels.UnresolvedAddressException 问题解决
最近写的 binlog2kafka storm job 上线在一个新的集群环境中(storm 0.9.0.1, kafka 0.8), storm job 运行时报出如下异常: java.lang.R ...
- 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 ...
- AES加解密异常java.security.InvalidKeyException: Illegal key size
AES加解密异常 Java后台AES解密,抛出异常如下:java.security.InvalidKeyException: Illegal key size Illegal key size or ...
- 异常-java.util.concurrent.TimeoutException: Futures timed out after [100000 milliseconds]
1 详细异常 java.util.concurrent.TimeoutException: Futures timed out after [100000 milliseconds] at scala ...
- 项目启动异常,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 ...
随机推荐
- nestjs中swagger的基本使用
nestjs中swagger的基本使用 安装 $ npm install --save @nestjs/swagger swagger-ui-express //如果使用fastify,则必须安装fa ...
- unable to access 'https://github.com/.../...git': Recv failure: Connection was reset
解决git下载报错:fatal: unable to access 'https://github.com/.../...git': Recv failure: Connection was rese ...
- js常用代码整理2
引用js <script type="text/javascript" src="js/jquery-1.11.2.min.js"></scr ...
- (转载)史上最详细的docker学习手册
原文链接:https://my.oschina.net/u/1388595/blog/5078146 一.docker入门 1.docker的安装及入门示例 环境准备:docker需要安装在cento ...
- 'xxx' must be unique because it is referenced by a foreign key.
'xxx' must be unique because it is referenced by a foreign key. 原因:在绑定外键时,对应的外键字段的没有设置成唯一. 说明:在定义字段时 ...
- 把userId:12323 直接拿到12323
JSONObject jsonObject1 = JSONObject.parseObject(mqttMessage); MessageVo messageVo = JSONObject.toJa ...
- vue项目,npm run dev的时候出现:Error: error:0308010C:digital envelope routines::unsupported
vue项目,npm run dev的时候出现:Error: error:0308010C:digital envelope routines::unsupported 这个是node的版本问题.我的n ...
- 89、des加密算法
import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpe ...
- spider_ip代理
title: spider_ip代理 author: 杨晓东 permalink: spider_ip代理 date: 2021-10-02 11:27:04 categories: - 投篮 tag ...
- js给ul下面所有的li追加类名