1. try-catch-finnaly
- try必须和catch或者finally组合使用;
public class TryDemoOne {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入第1个整数:");
int one = input.nextInt();
System.out.println("输入第2个整数:");
int two = input.nextInt();
input.close();
try{
System.out.println("one和two的商是:"+one/two);
} catch(Exception e) {
e.printStackTrace();
System.out.println("程序出错");
}
}
}
输入第1个整数:
0
输入第2个整数:
0
java.lang.ArithmeticException: / by zero
at learn.TryDemoOne.main(TryDemoOne.java:14)
程序出错
2. 多重catch
package learn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoOne {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入第1个整数:");
int one = input.nextInt();
System.out.println("输入第2个整数:");
int two = input.nextInt();
input.close();
try{
System.out.println("one和two的商是:"+one/two);
} catch(ArithmeticException e) { // 算术异常
e.printStackTrace(); // 输出异常信息
System.out.println("算术异常");
} catch (InputMismatchException e){
e.printStackTrace(); // 输出异常信息
System.out.println("输入异常");
} catch (Exception e){ // 需要放在最后
e.printStackTrace(); // 输出异常信息
System.out.println("发生异常");
} finally {
System.out.println("运算结束");
}
}
}
3. 终止finnaly运行
package learn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoOne {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入第1个整数:");
int one = input.nextInt();
System.out.println("输入第2个整数:");
int two = input.nextInt();
input.close();
try{
System.out.println("one和two的商是:"+one/two);
} catch(ArithmeticException e) {
System.exit(1); // 终止JVM运行的程序
e.printStackTrace();
System.out.println("算术异常");
} catch (InputMismatchException e){
e.printStackTrace();
System.out.println("输入异常");
} catch (Exception e){
e.printStackTrace();
System.out.println("发生异常");
} finally {
System.out.println("运算结束");
}
}
}
输入第1个整数:
12
输入第2个整数:
0
4. return关键字在异常处理中的作用
package learn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoTwo {
public static void main(String[] args) {
int result = test();
System.out.println("one和two的商是: "+ result);
}
public static int test() {
Scanner input = new Scanner(System.in);
try{
System.out.println("输入第1个整数:");
int one = input.nextInt();
System.out.println("输入第2个整数:");
int two = input.nextInt();
input.close();
return one/two;
} catch(ArithmeticException e) {
e.printStackTrace();
System.out.println("算术异常");
return 0;
} finally { // 会被强制执行
System.out.println("运算结束");
return -100000;
}
}
}
输入第1个整数:
36
输入第2个整数:
6
运算结束
one和two的商是: -100000
finally执行后才会执行return。无论输入什么,最后都返回-100000,删除finally的return后可以正常运算。
5. 使用throws
/* 通过throws抛出异常,针对可能的多种异常,解决方法:
1. throws后面接多个异常类型,中间用逗号分隔
2.
*/
方法一:throws后面接多个异常类型,中间用逗号分隔
package learn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoThree {
public static void main(String[] args) {
try{
int result = test();
System.out.println("one和two的商是: "+ result);
} catch (ArithmeticException e){
System.out.println("除数不允许为0");
e.printStackTrace();
} catch (InputMismatchException e){
System.out.println("输入类型异常");
e.printStackTrace();
}
}
public static int test() throws ArithmeticException, InputMismatchException{
Scanner input = new Scanner(System.in);
System.out.println("输入第1个整数:");
int one = input.nextInt();
System.out.println("输入第2个整数:");
int two = input.nextInt();
input.close();
return one/two;
}
}
package learn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryDemoThree {
public static void main(String[] args) {
try{
int result = test();
System.out.println("one和two的商是: "+ result);
} catch (ArithmeticException e){
System.out.println("除数不允许为0");
e.printStackTrace();
} catch (InputMismatchException e){
System.out.println("输入");
e.printStackTrace();
} catch (Exception e) {
}
}
/* 通过throws抛出异常,针对可能的多种异常,解决方法:
1. throws后面接多个异常类型,中间用逗号分隔
2. 直接接Exception
*/
public static int test() throws Exception{
Scanner input = new Scanner(System.in);
System.out.println("输入第1个整数:");
int one = input.nextInt();
System.out.println("输入第2个整数:");
int two = input.nextInt();
input.close();
return one/two;
}
}
6. 自定义的异常
package learn;
import java.util.Scanner;
public class ThrowsDemoOne {
public static void main(String[] args) {
testAge();
}
public static void testAge(){
try{
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if(age<18 || age>80){
throw new Exception("18岁以下或80岁以上需亲属陪同");
} else {
System.out.println("欢迎入住本酒店");
}
} catch (Exception e){
e.printStackTrace();
}
}
}
请输入年龄:
17
java.lang.Exception: 18岁以下或80岁以上需亲属陪同
at learn.ThrowsDemoOne.testAge(ThrowsDemoOne.java:16)
at learn.ThrowsDemoOne.main(ThrowsDemoOne.java:8)
package learn;
import java.util.Scanner;
public class ThrowsDemoOne {
public static void main(String[] args) {
try{
testAge();
} catch (Exception e){
e.printStackTrace();
}
}
public static void testAge() throws Exception{
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if(age<18 || age>80){
throw new Exception("18岁以下或80岁以上需亲属陪同");
} else {
System.out.println("欢迎入住本酒店");
}
}
}
7. 自定义异常类
package learn;
public class HotelAgeException extends Exception {
public HotelAgeException(){
super("18岁以下或80岁以上需亲属陪同");
}
}
package learn;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ThrowsDemoOne {
public static void main(String[] args) {
try{
testAge();
} catch(InputMismatchException e){
System.out.println("输入类型有误");
} catch (HotelAgeException e){
System.out.println("酒店前台不允许办理入住");
e.printStackTrace();
}
}
public static void testAge() throws HotelAgeException{
System.out.println("请输入年龄:");
Scanner input = new Scanner(System.in);
int age = input.nextInt();
if(age<18 || age>80){
throw new HotelAgeException();
} else {
System.out.println("欢迎入住本酒店");
}
}
}
8. throw和throws
throw 和 throws 是 Java 中用于处理异常的关键字,尽管它们的名称相似,但在使用和作用上有所不同。
- throw 关键字用于抛出一个异常对象。
- 它可以在代码中的任何位置使用,用于手动触发异常。
- 使用 throw 时,需要提供一个异常对象作为参数,该异常对象必须是 Throwable 类或其子类的实例。
public class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class Example {
public static void main(String[] args) {
try {
throw new CustomException("自定义异常消息");
} catch (CustomException e) {
System.out.println(e.getMessage());
}
}
}
throws 关键字:
- 用于在方法的声明中指定可能抛出的异常类型。它告诉调用该方法的代码,这个方法可能会抛出指定的异常,需要进行相应的异常处理或继续向上抛出异常。
以下是一个使用 throws 的示例:
public class Example {
public static void main(String[] args) throws CustomException {
test();
}
public static void test() throws CustomException {
// 在这个方法中抛出一个自定义异常
throw new CustomException("自定义异常消息");
}
}
总结:
- throw 用于手动抛出异常对象。
- throws 用于方法声明中指定可能抛出的异常类型。