复习java基础第二天(异常处理)
2024-08-26 11:07:25
一、常见的异常类型:
public class TestException { public static void main(String[] args) { int i = 10;
//数学异常: java.lang.ArithmeticException
int j = i / 0;
System.out.println(j); int [] scores = new int[]{1, 2, 4, 5};
//数组下标越界异常: java.lang.ArrayIndexOutOfBoundsException
System.out.println(scores[4]); Person p1 = new Man();
//类型转换异常: java.lang.ClassCastException
Woman p2 = (Woman) p1; p1 = null;
//空指针异常: java.lang.NullPointerException
System.out.println(p1.toString()); System.out.println("end...");
}
} class Person{ } class Man extends Person{ } class Woman extends Person{ }
二、不论在try、catch代码块中是否发生了异常事件,finally块中的语句都会被执行。
import java.io.FileNotFoundException;
import java.io.InputStream; public class TestTryCatchFinally { public static void main(String[] args) { try {
int i = 10;
int j = i / 0;
} finally{
System.out.println("finally...");
} //不论在try、catch代码块中是否发生了异常事件,finally块中的语句都会被执行。
System.out.println("end..."); //示例编译时异常, IO 异常属于编译时异常.
try {
InputStream is = new FileInputStream("abc.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
三、throws:
1. 在 Java 中使用 throws 关键字声明抛出异常.
2. throws 方法抛出的异常可以是方法中出现的异常的类型或其父类类型.
3. throws 可以声明抛出多个异常, 多个异常使用 , 分割.
4. 运行时异常不需要使用 throws 关键字进行显式的抛出.
5. 重写方法不能抛出比被重写方法范围更大的异常类型.
import java.io.FileNotFoundException;
import java.io.IOException; public class TestThrows { public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
} public static void test(){ int i = 10 / 0;
System.out.println(i); // InputStream fs = new FileInputStream("abc.txt"); // Connection connection = null;
// String sql = null;
// PreparedStatement ps = connection.prepareStatement(sql); // byte [] buffer = new byte[fs.available()];
// fs.read(buffer); A a = new B();
try {
a.method();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} class A{
void method () throws FileNotFoundException{
}
} class B extends A{
// @Override
// void method() throws IOException {
// }
}
四:throw:
人工手动抛出异常:
1. 创建一个异常类对象
2. 在方法内部使用 throw 关键字把该异常类对象抛出去!
自定义的异常类:
1. 通常继承自 RuntimeException(可以继承 Exception)
2. 自定义的异常类就是用来被人工抛出的!
import java.util.Scanner; public class TestThrow { public static void main(String[] args) {
try {
inputAge();
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("end...");
} /**
* 输入年纪: 要求年纪必须在 15-30 之间, 超出 30 则年纪偏大
*/
public static void inputAge(){
Scanner sc = new Scanner(System.in);
System.out.print("age=");
int age = sc.nextInt(); if(age > 30){
// System.out.println("年纪偏大.");
throw new AgeTooLargeException("年纪偏大.");
}
} public static void test(){ //1. 创建一个异常类对象
RuntimeException ex = new RuntimeException(); //2. 把异常类对象抛出去
throw ex;
}
}
五、自定义异常举例:
1、先定义一个异常类:
public class EcDef extends RuntimeException{ public EcDef() {
// TODO Auto-generated constructor stub
} public EcDef(String msg) {
super(msg);
} }
2、再创建一个处理异常的类:
public class EcmDef { public static void main(String[] args) { try {
int i = Integer.parseInt(args[0]); // "a"
int j = Integer.parseInt(args[1]); System.out.println(ecm(i, j));
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("输入的参数个数不足.");
} catch (ArithmeticException e) {
System.out.println("除数不能为 0");
} catch (EcDef e) {
System.out.println(e.getMessage());
} catch (NumberFormatException e) {
System.out.println("输入的参数不能转为整型.");
}
} public static int ecm(int i, int j){ if(i < 0 || j < 0){
throw new EcDef("不能处理负数. ");
} int result = i / j;
return result;
}
}
最新文章
- 《连载 | 物联网框架ServerSuperIO教程》- 12.服务接口的开发,以及与云端双向交互
- Nuget 命令 NuGet 管理项目库
- asp.net 的页面几种传值方式
- [Reomting Debug] 巧用VS 的remote debug 功能远程调试程序 经验分享.
- 关于ILDASM.EXE的知识整理
- [RabbitMQ] AMQP close-reason, initiated by Library, code=541
- 客户端禁用cookies后session是否还起效果
- Android 自定义ActionBar
- nginx_笔记分享_php-fpm详解
- SSH整合的详细步骤
- bzoj 3631: [JLOI2014]松鼠的新家
- 高淇java300集异常机制作业
- javascript 获取字符递增
- 关键字static
- Spring系列__02IOC模块简介
- pymysql的使用及sql注入
- Java容器解析系列(7) ArrayDeque 详解
- Nginx反向代理中使用proxy_redirect重定向url
- 一种很有意思的数据结构:Bitmap
- ajax回调中执行window.open被拦截的解决办法
热门文章
- MySql 日志查看与设置
- ubuntu下Apache2配置
- 《Java JDK 8 学习笔记》序
- scrapy——3 crawlSpider——爱问
- 神奇的幻方 noip2015day1 T1
- mysql5.7 简易修改mysql密码
- npm install socket.io 提示缺少";VCBuild.exe";
- [bzoj1812][IOI2006]riv_多叉树转二叉树_树形dp
- HDU 5245 上海大都会 J题 (概率期望)
- Linuxpassword破解及grub加密演示