JavaSE异常随笔
异常
异常是在程序执行期间发生的事件,该事件中断了程序指令的正常流程
public class Example1 {
public static void main(String[] args) {
calculate();
}
public static void calculate(){
int result = 1 / 0;
System.out.println(result);//该行代码不能够被执行
}
}
public Throwable();
public Throwable(String message);
public String getMessage();//获取异常发生的原因
public void printStackTrace(); //打印异常在栈中的轨迹信息

throw 抛出异常
**throw关键字只能在方法内部使用,throw关键字抛出异常表示自身并未对异常进行处理 **
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);//该行代码不能够被执行
}
}
throws 可以声明方法执行时可能抛出的异常类型,但需要注意的是:方法执行过程中只能抛出声明的 异常类型的其中一个异常。
try-catch 捕获异常
throw 和 throws 关键字均没有对异常进行处理,这可能会导致程序终止。
在这种情况下,可以使用try catch结构来对抛出异常进行捕获处理,从而保证程序能够正常运行
**当使用多个catch子句捕获异常时,如果捕获的多个异常对象的数据类型具有继承关系,那么父类异常 不能放在前面 **
finally 语句
finally 语句不能单独使用,必须与 try 语句或者 try-catch 结构配合使用,表示无论程序是否发生异常 都会执行,主要用于释放资源。**但如果在try语句或者catch语句中存在系统退出的代码,则 finally 语 句将得不到执行。 **
try{
} finally{
}
try{
} catch(异常类型 异常对象名){
} finally{
}
package try_catch_finally;
public class try_catch {
public static void main(String[] args) {
int result = getResult();
System.out.println(result);
}
public static int getResult() {
int number = 10;
try{
//临时变量 执行finally模块后再返回
return number;
}catch(Exception e) {
return 1;
}finally {
number++;
}
}
}
自定义异常
在Java中,异常的类型非常的多,要想使用这些异常,首先必须要熟悉它们。这无疑是一个巨大的工作 量,很耗费时间。如果我们可以自定异常,则只需要熟悉 RuntimeException 、Exception 和 **Throwable **即可。这大大缩小了熟悉范围。自定义异常还可以帮助我们快速的定位问题。
**自定义运行时异常语法 **
public class 类名 extends RuntimeException{}
**自定义检查异常语法 **
public class 类名 extends Exception{}
.
示例
package try_catch_finally;
/**
*
* 异常命名规范:场景描述+Exception
*/
public class usernameNotFoundException extends Exception
{
public usernameNotFoundException(){
}
public usernameNotFoundException(String msg){
super(msg);
}
}
package try_catch_finally;
/**
* 账号或密码错误异常
*/
public class BadCredentialsException extends Throwable {
public BadCredentialsException() {}
public BadCredentialsException(String msg) {
super();
}
}
package try_catch_finally;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.Scanner;
public class Login {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter your username: ");
String username = sc.next();
System.out.println("Enter your password: ");
String password = sc.next();
try {
login(username , password);
} catch (BadCredentialsException e) {
throw new RuntimeException(e);
} catch (usernameNotFoundException e) {
throw new RuntimeException(e);
}
}
public static void login(String username, String password) throws BadCredentialsException, usernameNotFoundException {
if("admin".equals(username)) {
if("123456".equals(password)) {
System.out.println("You have successfully logged in!");
}else{
throw new BadCredentialsException("账号或密码错误");
}
}else{
throw new usernameNotFoundException("账号不存在");
}
}
}
注意
a. 运行时异常可以不处理。
b. 如果父类抛出了多个异常,子类覆盖父类方法时,只能抛出相同的异常或者是该异常的子集。(与协变返 回类型原理一致)
c. 父类方法没有抛出异常,子类覆盖父类该方法时也不可抛出检查异常。此时子类产生该异常,只能捕获处理,不能声明抛出
父类中的方法没有声明抛出异常,子类中方法可以声明抛出运行时异常
父类中的方法没有声明抛出异常,子类中方法不能声明抛出检查异常

浙公网安备 33010602011771号