java(4) 异常
2024-09-30 10:17:25
1.Throwable 继承体系
* Eorro
* Exception
--RuntimeException 该类及其子类用于表示运行时异常,Exception类下所有其他子类都用于表示编译时异常。
--其他子类 2.Throwable 常用方法
* String getMessage() 返回此 throwable 的详细消息字符串
* void printStackTrace() 将此throwable 及其追踪输出至标准错误流
* void printStackTrace(PrintStream s) 将此throwable 及其追踪输出到指定的输出流 3.try ... catch
* 程序发生异常会立即终止,无法继续向下执行。为了解决这样的问题,Java中提供了一个中对异常进行处理的方式--异常捕获 class ExampleTest{ public static void main(String[] args){ try{
int result = divide(4,0);
System.out.println(result); }catch(Exception e){
System.out.println("捕获异常信息为:"+e.getMessage());
}
System.out.println("程序继续向下执行");
} public static int divide(int x,int y){
int result = x / y;
return result;
}
}
--对可能发生异常的代码用 try ... catch语句进行处理。
--在try代码中发生被0除的异常,程序会转而执行catch中的代码。
--在try代码中,发生异常语句后面的代码是不会被执行的【System.out.println(result);】。
--catch代码块对异常处理完毕后,程序仍会向下执行,而不会异常终止。语句【System.out.println("程序继续向下执行");】仍会被执行 4.finally
* 程序中有些语句无论程序是否发生异常都要被执行,这时就可以在try...catch语句后加一个finally代码块。
class ExampleTest{ public static void main(String[] args){ try{
int result = divide(4,0);
System.out.println(result); }catch(Exception e){
System.out.println("捕获异常信息为:"+e.getMessage());
return; //用于结束当前语句
}finally{
System.out.println("进入finally代码块");
}
System.out.println("程序继续向下执行");
} public static int divide(int x,int y){
int result = x / y;
return result;
}
}
--catch代码块中增加了return语句,用于结束当前方法,此时语句【System.out.println("程序继续向下执行");】就不会再被执行
--finally 中的代码块仍会被执行
--由于finally代码块的特性,通常用finally代码块释放系统资源 * finally代码块有一种情况下是不会被执行的,那就是在try...catch语句中执行了【System.exit(0)】,该语句表示退出当前虚拟机。 5.throws 关键字
-- 在上面的例子中,由于调用的是自己写的divide()的方法,因此很清楚该方法可能会发生异常。试想一下,如果是别人来调用该divide()方法,他如何判断该方法是否会产生异常及什么类型的异常?
-- 针对这种情况,java中允许在方法的后面使用throws关键字对外声明该方法有可能产生何种类型的异常。这样,调用者就明确知道该方法有异常,并且必须在程序中对异常进行处理,否则编译无法通过
-- throws 关键字声明抛出异常的语法格式:修饰符 返回值类型 方法名([参数1,参数2.....]) throws Exception1[,Exception2.......] class ExampleTest{ public static void main(String[] args){ int result = divide(4,2);
System.out.println(result);
} // 使用throws 关键字声明抛出异常
public static int divide(int x,int y) throws Exception{
int result = x / y;
return result;
}
} --由于定义divide()方法时声明抛出异常,但调用divide()方法时未做处理(异常捕获或在main方法级别继续throws),因此无法编译 class ExampleTest{ public static void main(String[] args){ try{
int result = divide(4,0);
System.out.println(result); }catch(Exception e){
System.out.println("捕获异常信息为:"+e.getMessage());
return; //用于结束当前语句
}
} // 使用throws 关键字声明抛出异常
public static int divide(int x,int y) throws Exception{
int result = x / y;
return result;
}
} 6.自定义异常
* 自定义异常类必须继承自Exception或其子类 //自定义一个异常类继承自 Exception
public class DivideByMinusException extends Exception { public DivideByMinusException() {
super(); //调用 Exception 无参的构造方法
} public DivideByMinusException(String message) {
super(message); //调用 Exception 有参的构造方法
} } * 自定义异常类的使用 需要用到关键字 throw,其格式:throw new 异常对象 public class ExampleTest { public static void main(String[] args) { int result = divide(4,-2);
System.out.println(result); } // 未使用throws 关键字声明抛出异常
public static int divide(int x,int y) {
if(y<0){
throw new DivideByMinusException ("被除数是负数");
} int result = x / y;
return result;
}
} --程序在编译时会发生异常。在一个方法内使用throw关键字抛出异常对象时,需要在该方法上使用throws关键字声明抛出异常。并在上一级调用中使用try...catch捕获异常 public class ExampleTest {
public static void main(String[] args) { int result;
try {
result = divide(4,-2);
System.out.println(result);
} catch (DivideByMinusException e) {
e.printStackTrace();
}
} // 使用throws 关键字声明抛出异常
public static int divide(int x,int y) throws DivideByMinusException {
if(y<0){
throw new DivideByMinusException ("被除数是负数");
} int result = x / y;
return result;
} }
最新文章
- Always review
- sql基础查询
- Install Hive
- JSP Servlet SQL 三者之间数据传递
- QT renered html for js
- 算法起步之Prim算法
- NanoApe Loves Sequence Ⅱ(尺取法)
- Vue源码后记-其余内置指令(2)
- 用SpeedFan来控制CPU风扇转速
- bootstrap 一个简单的登陆页面
- Docker多台物理主机之间的容器互联
- explode() 字符串分割函数
- java一些必会算法
- 一种模块化开发的目录结构和部署tips
- Python是什么
- 基于RESTful API 设计用户权限控制
- 【Lua】面向对象编程(二)
- 20145234黄斐《java程序设计》第二周
- 域名 DNS命令——dig
- iOS 文件和数据管理 (可能会删除本地文件储存)