package com.Lucky.oop;
import java.io.IOException;
import static com.Lucky.oop.DefindsException.Add;
/*
异常:
throwAble: error 与 exception
Error:由java虚拟机生成并且抛出,大多数错误与编写者的操作无关
例如:OutOfMemoryError[内存溢出] 或 NoClassDefFoundError
或 LinkageError[连接错误]
exception:关键字:try catch finally throw throws
IOException[IO异常]
例如:FileNoFoundException[文件找不到异常]
…………………………
runtimeException[运行时异常]
例如:ArrayIndexOutOfBoundsException[数组下标越界异常]
NullPointerException[空指针异常]
ArithmeticException[算术异常]
ClassNoFoundException[找不到类异常]
…………………………
每天小知识:ctrl+alt+T :生成常用的判断
*/
public class ExceptionClass {
public static void main(String[] args) {
try{ //监控区
}catch (ArithmeticException e){
////要捕获的异常:从小到大,不然大的异常就包括小的异常了,将无法捕获小的异常了
// ArithmeticException<Exception<Throwable
}catch (Exception u){
u.printStackTrace(); //异常信息
}catch (Throwable e){
}finally { //finally :常用于 IO,资源关闭
System.out.println("finally最后执行的【必然会执行的】");
}
String strA="病毒警告";
try {
Add(strA);
} catch (DefindsException e) {
e.printStackTrace();
} finally {
}
}
}
自定义异常:
package com.Lucky.oop;
/*
自定义异常:只要继承Exception即可
*/
public class DefindsException extends Exception{
private String message;
//相当于有参构造器
public DefindsException(String message) {
this.message = message;
}
//异常处理方法
public static void Add(String str)throws DefindsException{
if(str=="病毒警告"){
throw new DefindsException(str);
}
System.out.println("ok");
}
@Override
public String toString() {
return "DefindsException{" +
"message='" + message + '\'' +
'}';
}
}