java 7异常

Throwable

是所有异常的父类。其常用方法如下:

java 复制代码
public Throwable();
public Throwable(String message);
public String getMessage();//获取异常发生的原因
public void printStackTrace(); //打印异常在栈中的轨迹信息

第二节 异常处理

  1. 如何处理异常
    在Java中,异常的种类很多,如果每一种异常类型我们都需要去记住,这无疑是一件很困难的事。如果
    能够有一套机制来处理异常,那么将减少程序员在开发时的耗时。Java就提供了一套异常处理机制来出
    来异常。Java处理异常使用了5个关键字:throw 、throws、try、catch、finally
  2. throw 抛出异常
    throw关键字只能在方法内部使用,throw关键字抛出异常表示自身并未对异常进行处理。
java 复制代码
package com.cyx.exception;
import java.util.Scanner;
public class Example1 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
calculate();
}
public static void calculate(){
System.out.println("请输入一个数字:");
int number1 = sc.nextInt();
System.out.println("请再输入一个数字:");
int number2 = sc.nextInt();
if(number2 == 0){
ArithmeticException e = new ArithmeticException("在除法运算中除数
不能为零");
throw e;
}
int result = number1 / number2;
System.out.println(result);//该行代码不能够被执行
}
}
```![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/452d7cc5c53144b7973f395560190aea.jpeg#pic_center)
注意到:当被除数为0,又无解决方案时,会往上级寻找解决方案,代码未给解决方案,程序退出(返回值为1);

3. throws 声明可能抛出的异常类型
throws关键字只能应用在方法或者构造方法的定义上对可能抛出的异常类型进行声明,自身不会对异常
做出处理,由方法的调用者来处理。如果方法的调用者未处理,则异常将持续向上一级调用者抛出,直
至main()方法为止,如果main()方法也未处理,那么程序可能因此终止

```java
package com.cyx.exception;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Example2 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int result = devided();
System.out.println(result);
}
public static int devided() throws InputMismatchException,
ArithmeticException{
int number1 = getNumber();
int number2 = getNumber();
return number1 / number2;
}
/**
*
* @return
* @throws InputMismatchException 执行该方法时可能会抛出
InputMismatchException
*/
public static int getNumber() throws InputMismatchException {
System.out.println("请输入一个数字:");
int number = sc.nextInt();
return number;
}
}
  1. try-catch 捕获异常
    throw 和 throws 关键字均没有对异常进行处理,这可能会导致程序终止。在这种情况下,可以使用trycatch结构来对抛出异常进行捕获处理,从而保证程序能够正常运行。
java 复制代码
package com.cyx.exception;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Example2 {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int result = devided();
System.out.println(result);
}
public static int devided() throws InputMismatchException,
ArithmeticException{
int number1 = getNumber();
int number2 = getNumber();
return number1 / number2;
}
/**
*
* @return
* @throws InputMismatchException 执行该方法时可能会抛出
InputMismatchException
*/
public static int getNumber() throws InputMismatchException {
System.out.println("请输入一个数字:");
int number = sc.nextInt();
return number;
}
}
相关推荐
斯内普吖1 小时前
(开源)线上花店系统实战指南 基于 Java + SSM + Vue + MySQL
java·vue.js·mysql·开源
duxingzhe03111 小时前
我的一些书
java·数据库·tomcat·数据库开发·etl工程师
2603_965148111 小时前
商品价格监控 API 接口:从原理到实战的完整指南
java·前端·数据库·python·生活
瑞克-桑切斯1 小时前
2、Shell 条件语句
java·linux·服务器
Pniubi1 小时前
分片的意义?
java·开发语言
酷虎软件1 小时前
视频加字幕怎么制作
java·前端·音视频
Wang's Blog2 小时前
Java 项目实战: 交友项目 - 阿里云概述与项目中的云服务选型
java·阿里云·交友
阿皮 API2 小时前
Shopee虾皮商品详情API接口调用实战:Python获取东南亚商品数据与JSON解析
开发语言·python·json
niucloud-admin2 小时前
NIUCLOUD LITE AI开发文档--系统部署
开发语言·ai