问题分类
错误
Error 错误不能算为异常,因为它,程序员是没办法处理的。例如:内存不足,硬盘空间不够,网线被耗子拆了,服务器停电了……
异常
编译期异常
指在编写代码过程,由编译期主动识别出来的问题,就是编译期异常
运行时异常
指在编写代码过程中,编译期识别不出来,但是在运行期由JVM抛出来的问题
异常的解决方案
运行时异常解决方案
编译时异常解决方案
编译时异常有3种解决方案:
1、捕获这个异常,不让它沿着调用栈继续向下抛出
- ``
private static void method02() {try {//编译时异常File file = new File("D://test.txt");FileInputStream in = new FileInputStream(file);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}
2、捕获这个异常,并继续向下抛出;
private static void method02() throws Exception{try {//编译时异常File file = new File("D://test.txt");FileInputStream in = new FileInputStream(file);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();throw e;}}
3、不捕获这个异常,从而导致method1()方法从调用栈中被弹出,异常对象继续抛给调用栈下面的main()方法。
private static void method02() throws Exception{//编译时异常File file = new File("D://test.txt");FileInputStream in = new FileInputStream(file);}
怎么识别异常?

凡是直接继承Exceptioin类,或者是它子类的派生几乎都是编译时异常
凡是直接继承RuntimeException类, 或者是它子类的派生都是运行时异常
如何捕获异常
语法:
try {//监控代码,抛不抛异常}catch(Exception e){//如何解决异常}
案例:
private static void method02() {System.out.println("开始");// 编译时异常File file = new File("D://test.txt");- ``
try {FileInputStream in = new FileInputStream(file);//如果上述代码,抛出异常,下面的代码不会执行System.out.println("asdfasdfasdfasdasdf");- ``
} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("抛异常……");}- ``
System.out.println("结束");-
自定义编译时异常
extends Exception完事
/*** 自定义 编译期异常* @author 蜗牛学院**/public class InitException extends Exception{- ``
public InitException() {super();// TODO Auto-generated constructor stub}- ``
public InitException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);// TODO Auto-generated constructor stub}- ``
public InitException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}- ``
public InitException(String message) {super(message);// TODO Auto-generated constructor stub}- ``
public InitException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}}/*** 自定义 编译期异常* @author 蜗牛学院**/public class InitException extends Exception{- ``
public InitException() {super();// TODO Auto-generated constructor stub}- ``
public InitException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);// TODO Auto-generated constructor stub}- ``
public InitException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}- ``
public InitException(String message) {super(message);// TODO Auto-generated constructor stub}- ``
public InitException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}}
如何使用异常?
需要定义一个方法,在方法中使用throw 将异常抛出,但是一定要结合throws对该方法进行异常定义
private static int add(int a,int b) throws InitException {if(a < 0 || b < 0) {throw new InitException("传入的参数为:负数");}- ``
return a + b;- ``
}- ``
public static void main(String[] args) {// TODO Auto-generated method stub- ``
try {add(4,5);} catch (InitException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
<br />
