面向对象第五天
异常
异常:指的是程序在运行的过程中发生的不正常事件
面试:Exception,error,Throwable
-
Exception和error都是Throwable派生出来的两个子类
-
error:属于程序本身造成的无法恢复的错误,例如内存溢出
-
Exception:java应用程序抛出的非严重的错误
主要是java代码中出现了一些像(null这样的对象,数组越界等)
注意:针对java应用程序抛出的错误,我们通常会使用try-catch语句块对该错误进行捕获
–NullPointerException
try-catch语句块对该错误进行捕获
面试:final和finally的区别?
-
-final是用来修饰变量,方法,类的
-
-finally是和try-catch语句块连用的,表示无论如何都一定会执行的代码块
-
注意:finally中的代码通常是用来关闭一些连接或者流的
System.exit(1):唯一能让finally不执行的代码
try-catch
/** * 预计下面的代码有可能会抛出一个算数异常 * try-catch语句块的格式: * try{ * 预计会抛出异常的代码块; * }catch(异常类型 异常变量){ * 如果捕获到异常,那么对异常进行处理的代码块; * (一般都是给一段提示语) * } * * * 注意: * 1.一旦在try里面抛出了异常,那么抛出的异常语句块下面的 * 代码就不会继续执行了 * 2.try-catch结构中,可以有多个catch语句块,代表着可以 * 精确的捕获多种异常,但是要注意,越往后面的catch, * 捕获的异常类型一定要比前面的异常类型要大 * 3.通常情况下,我们使用try-catch并不是主动的,而是被动的 * 4.try-catch语句块是可以没有catch,但是必须在try的后面 * 加上finally语句块 */
面试:throw和throws的区别?
-
-throw是用来在方法中抛出异常对象的(new出来的)
-
-throws是用来在方法的后面声明该方法会抛出的异常类型
Demo1
public class Demo1 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
//System.out.println(num);
int a=10;
int b=0;
/**
* 预计下面的代码有可能会抛出一个算数异常
* try-catch语句块的格式:
* try{
* 预计会抛出异常的代码块;
* }catch(异常类型 异常变量){
* 如果捕获到异常,那么对异常进行处理的代码块;
* (一般都是给一段提示语)
* }
*
*
* 注意:
* 1.一旦在try里面抛出了异常,那么抛出的异常语句块下面的
* 代码就不会继续执行了
* 2.try-catch结构中,可以有多个catch语句块,代表着可以
* 精确的捕获多种异常,但是要注意,越往后面的catch,
* 捕获的异常类型一定要比前面的异常类型要大
* 3.通常情况下,我们使用try-catch并不是主动的,而是被动的
* 4.try-catch语句块是可以没有catch,但是必须在try的后面
* 加上finally语句块
*/
int num=0;
String str=null;
try {
num=a/b;
str.length();
System.out.println("123"+num);
} catch (ArithmeticException e) {
//日志打印 txt
System.out.println("除数为零了!");
}catch(NullPointerException e2){
System.out.println("空指针异常!");
}
System.out.println(num);//ArithmeticException
try {
print();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 注意:一般情况下,有throw就必定会有throws,
* 但是当我们throw的异常对象是RuntimeException或者是他的
* 子类异常对象的时候,我们是可以省略掉throws的
* @throws ClassNotFoundException
*/
public static int print() throws ClassNotFoundException{
int a=10;
int b=1;
if(b==0){
throw new ClassNotFoundException();
}
return a/b;
}
}
常见异常
-
NullPointerException:空指针异常
-
ArrayIndexOutOfBoundsException:数组下标越界异常
-
ClassNotFoundException:没有找到指定的类异常(路径不对)
-
InputMismatchException:输入不匹配异常
-
ClassCastException:类型转换异常(强制转换)
-
NumberFormatException:数字转换异常
-
IllegalArgumentException:接收到非法参数异常
-
RuntimeException和Checked
-
RuntimeException:运行时异常
-
Checked:非运行时异常
public class Demo2 {
public static void main(String[] args) {
int a=10;
int b=0;
try {
System.out.println("11");
/**
* System.exit(1):唯一能让finally不执行的代码
*/
//System.exit(1);
//return;
int num=a/b;
System.out.println(num);
} catch (Exception e) {
/**
* e.printStackTrace():
* 调用程序自带的异常信息提示
*/
//System.out.println("除数为0");
e.printStackTrace();
/*String str=e.getMessage();
System.out.println(str);*/
}finally{
System.out.println("一定执行的代码块");
}
System.out.println("执行完毕");
}
}
自定义异常:性别异常
- 1.创建一个自定义的异常类
- 2.继承Exception异常类
- 3.重写里面的所有方法(需要进入到Exception类中复制,改名字)
public class Demo3 {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入性别:");
String sex=scan.next();
try {
printSex(sex);
} catch (SexException e) {
System.out.println("性别异常");
e.printStackTrace();
}
}
public static void printSex(String str) throws SexException{
if("男".equals(str)){
System.out.println("输入匹配");
}else{
throw new SexException("性别不对");
}
}
}
创建一个SetException继承Exception
public class SexException extends Exception{
//序列化的版本号
private static final long serialVersionUID = 5657369801130211637L;
public SexException() {
super();
}
public SexException(String message) {
super(message);
}
public SexException(String message, Throwable cause) {
super(message, cause);
}
public SexException(Throwable cause) {
super(cause);
}
protected SexException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}