代码拉取完成,页面将自动刷新
同步操作将从 flatfish/Java-Review 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// 获取异常的信息
public String getMessage() {
return detailMessage;
}
// 获取异常的原因
public String getLocalizedMessage() {
return getMessage();
}
// 返回当前异常是否是指定的异常,如果不是就返回null
public synchronized Throwable getCause() {
return (cause==this ? null : cause);
}
// 打印异常信息
public void printStackTrace() {
printStackTrace(System.err);
}
// toString() 方法
public String toString() {
String s = getClass().getName();
String message = getLocalizedMessage();
return (message != null) ? (s + ": " + message) : s;
}
可以在try(资源1,资源2){}catch{} 的 () 中释放异常
在哪些情况下,finally不会执行?
在finally块执行之前退出程序 System.exit(1)
public class ExceptionDemo {
public static void main(String[] args) {
try {
int i = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
System.exit(1);
} finally {
System.out.println("ExceptionDemo.main");
}
}
}
在finally块的第一句就是退出程序,则剩下的finally块不会执行
public class ExceptionDemo {
public static void main(String[] args) {
try {
int i = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
} finally {
System.exit(1);
System.out.println("ExceptionDemo.main");
}
}
}
在finally块的第一行出现异常
public class ExceptionDemo {
public static void main(String[] args) {
try {
int i = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
} finally {
int i = 1 / 0;
System.out.println("ExceptionDemo.main");
}
}
}
注意:此处所说的 System.exit() 并不是一定会执行,如下列代码,执行执行了System.exit(1)之后,程序会直接退出,如下代码
public class ExceptionDemo {
public static void main(String[] args) {
try {
int i = 1 / 0;
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
} finally {
System.out.println("ExceptionDemo.main");
}
}
}
Java 异常 - OOM异常.md
详细讲解// 1. 创建一个类
// 2. 继承 Exception
// 3. 生成构造方法、重写方法即可
public class MyException extends Exception{
public MyException() {
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
@Override
public String getMessage() {
return "别胡扯异常";
}
}
// ----------------------------------------------------------------------------------
public class MyException2 extends Throwable{
public MyException2() {
}
public MyException2(String message) {
super(message);
}
public MyException2(String message, Throwable cause) {
super(message, cause);
}
public MyException2(Throwable cause) {
super(cause);
}
public MyException2(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
@Override
public String getMessage() {
return "第二个别胡扯异常";
}
}
// 测试
public class TestMyException {
public static void main(String[] args) throws MyException2{
try {
if (10 > 0) {
throw new MyException2("eeee");
}
} catch (MyException e) {
e.printStackTrace();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。