错误:error
异常Exception
: 异常不是错,是意外
- 分类
- 运行异常(未检查异常)
:可处理,可不处理
RuntimeException以及子类都为运行异常
- 编译异常(检查异常)
:必须处理
Exception以及子类(不包括RuntimeException)都是编译异常
-
处理方式:
-
抛出异常 throws
在方法名后 抛出 throws 异常类型
-
try。。。catch
try{ 可能出现异常的代码块; }catch(异常类型 异常对象){ //异常处理 }finally{//可以没有finally //保证必须执行的代码 }
代码案例
import java.util.Scanner;
public class Age {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
int age = sc.nextInt();
if(age>100) {//年龄过大异常
//创建一个异类对象,抛出异常
throw new Max();
}else if(age<0) {
try {
throw new Min();
} catch (Min e) {//年龄过小异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("你的年龄是 "+age);
}
}
package com.zking.test;
/**
* 异常的俩处理方式
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test1 {
public static void main(String[] args){
System.out.println(10/2);
System.out.println(10/5);
//0不能作为被除数
// System.out.println(10/0);
//ArithmeticException: 运算异常
int[] a = new int[10];
System.out.println(a[0]);
System.out.println(a[9]);
//超出范围
// System.out.println(a[10]);
//ArrayIndexOutOfBoundsException: 下标索引越界
Student s = null;//创建学生对象 为空
// s.name = "嘿嘿嘿";
//NullPointerException: 空指针异常(对象是空的)
// public void add(){
// SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日");//月日大 D,横线小 d
// //创建时间对象
// Date date = new Date();
// //第一种
// System.out.println(sdf.format(date));
// //第二种
// String strDate = sdf.format(date);
// System.out.println(strDate);
// sdf.parse(strDate);
// }
//第二种处理方式
SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日");
Date date = new Date();
String strDate = sdf.format(date);
System.out.println(strDate);
try {
sdf.parse(strDate);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//方法不能相互,应该是平级关系
//第一种处理方式 throws
public void add() throws ParseException {//出现0
SimpleDateFormat sdf = new SimpleDateFormat("YYYY年MM月dd日");
//创建时间对象
Date date = new Date();
String strDate = sdf.format(date);
System.out.println(strDate);
sdf.parse(strDate);
}
}
- 自定义异常
-
使用:
throw 异常对象
eg.throw new EC();
-
方法:
1、创建类 继承异常类 2、通过构造函数 构造新的异常信息
代码案例
package com.zking.test;
import java.util.Scanner;
public class Age {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
int age = sc.nextInt();
if(age>100) {//年龄过大异常
//创建一个异类对象,抛出异常
throw new Max();
}else if(age<0) {
try {
throw new Min();
} catch (Min e) {//年龄过小异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("你的年龄是 "+age);
}
}
package com.zking.test;
/**
* 年龄太大异常
* @author Administrator
*
*/
public class Max extends RuntimeException{
public Max() {
super("年龄太大了,老苟");
}
}
package com.zking.test;
/**
* 年龄太小异常
* @author Administrator
*
*/
public class Min extends Exception{
public Min() {
super("(⊙_⊙)? Are you god?");
}
}
-常见异常:
ArithmeticException:算术异常
ArrayIndexOutOfBoundsException:下标索引越界
NullPointerException:空指针异常(对象是空的)
ParseException:解析异常
package com.zking.test;
import java.util.Scanner;
/**
*
* @author Administrator
*
*/
public class Test2 {
public static void main(String[] args) {
//1
try {
System.out.println(10/0);
} catch (Exception e) {//这里写可能出现异常的代码
//错误输出流
// TODO: handle exception
System.err.println("0不能做被除数");//出现错误的处理方式
}finally{
System.out.println("这里是必须执行代码");//这里的代码必须执行
}
System.out.println("hhh");
System.out.println("=============");
//2
try {
int[]a = new int[5];
System.out.println(a[6]);
} catch (Exception e) {
// TODO: handle exception
System.err.println("下标越界");
}
System.out.println("==============");
//3
Scanner sc = new Scanner(System.in);
try {
int a = sc.nextInt();
System.out.println(a);
} catch (Exception e) {
// TODO: handle exception
System.err.println("你似布似sa,输入int啦");
}
}
}