异常的产生
当遇到异常,执行到异常语句时,程序会中断执行,后面的也不会执行,
出现异常。
public class Love{
public static void main(String []args){
System.out.println("1");
System.out.println("2"+(10/0));
System.out.println("3");
}
}
----------------------------------------------------------
F:\test>java Love
1
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Love.main(Love.java:4)
处理异常(try...catch)
组合方式:try...catch、try...catch...finally、try...finally
public class Love{
public static void main(String []args){
System.out.println("1");
try{
System.out.println("2"+(10/0));
}catch(ArithmeticException e){
System.out.println("Error");
}
System.out.println("3");
}
}
--------------------------------------------------
F:\test>java Love
1
Error
3
输出异常信息(使用printStackTrace();)
public class Love{
public static void main(String []args){
System.out.println("1");
try{
System.out.println("2"+(10/0));
}catch(ArithmeticException e){
e.printStackTrace();
}
System.out.println("3");
}
}
--------------------------------------------------------------
F:\test>java Love
1
java.lang.ArithmeticException: / by zero
at Love.main(Love.java:5)
3
处理异常(try...catch...finally)
不管出不出异常,都会执行
public class Love{
public static void main(String []args){
System.out.println("1");
try{
System.out.println("2"+(10/0));
}catch(ArithmeticException e){
e.printStackTrace();
}finally{
System.out.println("usually");
}
System.out.println("3");
}
}
-------------------------
F:\test>java Love
1
java.lang.ArithmeticException: / by zero
at Love.main(Love.java:5)
usually
3
--------------------------------------------------
public class Love{
public static void main(String []args){
System.out.println("1");
try{
System.out.println("2"+(10/1));
System.out.println("usually");
}catch(ArithmeticException e){
e.printStackTrace();
}finally{
System.out.println("usually");
}
System.out.println("3");
}
}
-----------------------------------
F:\test>java Love
1
210
usually
usually
3
一、出现异常 用户什么都不输入(F:\test>java Love)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Love.main(Love.java:5)
二、用户输入的参数不是数字(F:\test>java Love a b)
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Love.main(Love.java:5)
三、被除数为0(F:\test>java Love 10 0)已处理
F:\test>java Love 10 0
1
java.lang.ArithmeticException: / by zero
at Love.main(Love.java:7)
usually
3
统一处理
public class Love{
public static void main(String []args){
System.out.println("1");
try{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
System.out.println("2"+(x/y));
System.out.println("usually");
}catch(ArithmeticException e){
e.printStackTrace();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}catch(NumberFormatException e){
e.printStackTrace();
}finally{
System.out.println("usually");
}
System.out.println("3");
}
}
异常的处理流程
下面是两个异常类的继承结构
可以发现所有的异常都是Throwable的子类,而Throwable下有两个子类。
Error和Exception的区别
Error:JVM错误,此时的程序还没有执行,如果没有执行用户,则无法处理;
Exception:程序运行过程中产生的异常,用户可以处理。
Java异常处理流程
使用Exception处理异常
Exception包含了子类异常,可以全处理
public class Love{
public static void main(String []args){
System.out.println("1");
try{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
System.out.println("2"+(x/y));
System.out.println("usually");
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("usually");
}
System.out.println("3");
}
}
throws关键字
用throws抛出异常后,不管有没有异常,都要用try处理;不要在主方法上加throws Exception,可以加。
class MyMath{
public static int div(int x,int y)throws Exception{
return x/y;
}
}
public class Love{
public static void main(String []args){
try{
System.out.println(MyMath.div(10,2));
}catch(Exception e){
e.printStackTrace();
}
}
}
------------------------------------------------------------------
class MyMath{
public static int div(int x,int y)throws Exception{
return x/y;
}
}
public class Love{
public static void main(String []args)throws Exception{
System.out.println(MyMath.div(10,2));
}
}
throw(手工抛异常)
public class Love{
public static void main(String []args){
try{
throw new Exception("Error");
}catch(Exception e){
e.printStackTrace();
}
}
}
throw和throws区别
异常处理标准格式
正常代码
class MyMath{
public static int div(int x,int y){
int result = 0;
System.out.println("Start");
result = x/y;
System.out.println("End");
return result;
}
}
public class Do{
public static void main(String []args){
System.out.println(MyMath.div(10,2));
}
}
产生异常(出现异常后,后面的语句都不会执行,而是顺着throws抛给了主方法)
package test;
class MyMath{
public static int div(int x,int y)throws Exception{
int result = 0;
System.out.println("Start");
result = x/y;
System.out.println("End");
return result;
}
}
public class Do{
public static void main(String []args){
try{
System.out.println(MyMath.div(10,0));
}catch(Exception e){
e.printStackTrace();
}
}
}
----------------------------
Start
java.lang.ArithmeticException: / by zero
at test.MyMath.div(Do.java:7)
at test.Do.main(Do.java:15)
处理异常(异常出现后,继续抛)
//标准
package test;
class MyMath{
public static int div(int x,int y)throws Exception{
int result = 0;
System.out.println("Start");
try{
result = x/y;
}catch(Exception e){
throw e;
}finally{
System.out.println("End");
}
return result;
}
}
public class Do{
public static void main(String []args){
try{
System.out.println(MyMath.div(10,0));
}catch(Exception e){
e.printStackTrace();
}
}
}
------------------------------------------------------------------
//交给主方法处理
package test;
class MyMath{
public static int div(int x,int y)throws Exception{
int result = 0;
System.out.println("Start");
try{
result = x/y;
}finally{
System.out.println("End");
}
return result;
}
}
public class Do{
public static void main(String []args){
try{
System.out.println(MyMath.div(10,0));
}catch(Exception e){
e.printStackTrace();
}
}
}
Runtime类
观察代码
public class Do{
public static void main(String []args){
int temp = Integer.parseInt("100");
System.out.println(temp);
}
}
观察parseInt方法
NumberFormatException继承结构
Exception和RuntimeException的区别