java异常处理机制示例(java抛出异常、捕获、断言)
时间:2022-11-16 21:26:13
这是一个介绍基本异常处理的小例子,包括抛出,捕获,断言,日志。
Java异常处理通过5个关键字try、catch、throw、throws、finally进行管理。基本过程是用try语句块包住要监视的语句,如果在try语句块内出现异常,则异常会被抛出,你的代码在catch语句块中可以捕获到这个异常并做处理;还有以部分系统生成的异常在Java运行时自动抛出。你也可以通过throws关键字在方法上声明该方法要抛出异常,然后在方法内部通过throw抛出异常对象。
package com.hongyuan.test;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ExceptionHandleTest {
static{
//开启断言,此后由系统类加载器加载的类将启用断言。
ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
}
public static void main(String[] args) {
/*
* 抛出,捕获
*/
try {
TryCatchTest.run(10, -1);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("====================================================");
//日志
LogerTest.run();
System.out.println("====================================================");
//断言
AssertTest.div(3,0);
}
}
/*
* 断言
*/
class AssertTest {
public static double div(int b,int a){
assert a!=0:"你这么用,你小学老师知道吗?";
return (double)b/a;
}
}
/*
* 日志
*/
class LogerTest {
private static Logger logger=null;
static{
//获取日志对象并定义日志级别
logger=Logger.getLogger(LogerTest.class.getName());
logger.setLevel(Level.ALL);
}
public static void run(){
//进入方法
logger.entering(LogerTest.class.getName(), "run");
//普通信息
logger.info("又来找我麻烦,这笔账我记下了!!!");
//警告
logger.warning("太累了,这活没法干了!!!");
//严重
logger.log(Level.SEVERE,"老子不干了!!! ^O^");
//退出方法
logger.exiting(LogerTest.class.getName(), "run");
}
}
/*
* 捕获,抛出
*/
class TryCatchTest {
public static void run(int x,int y) throws IOException {
try{//必须
if(x<0||y<0){
throw new IllegalArgumentException("无语了,这让我怎么办啊!!!");
}
}catch(Exception e){//可选
IOException e1=new IOException("你自己看着办吧!");
e1.initCause(e.getCause());
throw e1;
}finally{//可选
System.out.println("最后他们过上了幸福的生活!!!!(完)");
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 微信摇一摇功能实现详细介绍
Java 如何实现时间控制
Java使用自动化部署工具Gradle中的任务设定教程
![](https://img.aspxhome.com/file/2023/7/89577_0s.png)
OpenCV实现图像背景虚化效果原理详解
![](https://img.aspxhome.com/file/2023/2/109782_0s.jpg)
Spring @value用法示例详解
Android实现系统级悬浮按钮
![](https://img.aspxhome.com/file/2023/6/138796_0s.png)
Android 蓝牙连接 ESC/POS 热敏打印机打印实例(ESC/POS指令篇)
![](https://img.aspxhome.com/file/2023/3/138193_0s.png)
理解Java多线程之并发编程
![](https://img.aspxhome.com/file/2023/1/125581_0s.png)
C#中Dispose和Finalize方法使用介绍
Java BigDecimal案例详解
![](https://img.aspxhome.com/file/2023/9/63509_0s.png)
C#文件上传的简单实现
![](https://img.aspxhome.com/file/2023/1/102291_0s.jpg)
通过Java设置Word页面背景色过程详解
![](https://img.aspxhome.com/file/2023/2/68702_0s.png)
Mybatis plus多租户方案的实战踩坑记录
![](https://img.aspxhome.com/file/2023/6/57956_0s.jpg)
C#使用doggleReport生成pdf报表的方法
SpringBoot使用GraphQL开发Web API实现方案示例讲解
![](https://img.aspxhome.com/file/2023/8/85418_0s.png)
Android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法
![](https://img.aspxhome.com/file/2023/6/90126_0s.jpg)
深入谈谈C#9新特性的实际运用
Spring Boot2如何构建可部署的war包
![](https://img.aspxhome.com/file/2023/5/60365_0s.png)
不规范使用ThreadLocal导致bug分析解决
![](https://img.aspxhome.com/file/2023/2/59182_0s.png)
android使用OkHttp实现下载的进度监听和断点续传
![](https://img.aspxhome.com/file/2023/9/98639_0s.gif)