异常有
-
Error: 严重错误Error,无法通过处理的错误,只能事先避免,好比绝症。
-
Exception:表示异常,异常产生后程序员可以通过代码的方式纠正,使程序继续运行,是必须要处理的。好比感冒
由于Error无法避免,我们上面说的异常处理机制主要是针对Exception。
Exception:
异常分类
运行时异常:在编译时不会报错,但是运行后会报错(如:空指针异常、算数错误1/0)
编译时异常:在编译时候就出现的时候错误,需要处理,不处理会直接编译错误。
异常处理
五个处理异常:try、catch、finally、throw、throws
thow异常:
throw用在方法内,用来抛出一个异常对象,将这个异常对象传递到调用者处,并结束当前方法的执行。
public static void main(String[] args) {
int [] arr ={1,2,3,4};
int index =4;
int elemet= getElement(arr,index);
System.out.println(elemet);
}
private static int getElement(int[] arr , int index) {
if(index<0 || index>arr.length-1){
throw new ArrayIndexOutOfBoundsException("blb提醒您,数组下标已越界。");
}
return arr[index];
}
thows:
自己不处理直接丢给上级处理。
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse("2020-10-10");
}
这是jvm自己给出的错误提示
try-catch捕获异常:
try
块中写编写可能产生异常的代码。
catch
块中用来表示发生了异常,如何进行处理的逻辑。
public class Demo2 {
public static void main(String[] args) {
System.out.println(demo());
}
public static String demo(){
try {
System.out.println(1/0);
return "b";
} catch (Exception e) {
System.out.println("出错了");
// System.exit(0);
}finally {
System.out.println("a");
return "d";
}
}
}
finally代码块:
必须要走的区块
try {
System.out.println(1/0);
return "b";
} catch (Exception e) {
System.out.println("出错了");
// System.exit(0);
}finally {
System.out.println("a");
return "d";
}
自定义异常:
首先定义一个类去继承RuntimeException类
public class InPutException extends RuntimeException{
public InPutException() {
}
public InPutException(String message) {
super(message);
}
}
写一个JavaBean类
package com.day18;
public class Person {
int age;
char sex;
public Person() {
}
public Person(int age, char sex) {
setAge(age);
setSex(sex);
}
public int getAge() {
return age;
}
public void setAge(int age) {
// 如果年龄为负数抛出此异常
if(age>0&&age<120){
this.age = age;
}else {
throw new InPutException("年龄超出120或者小于0岁");
}
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
if (sex=='男'||sex=='女'){
this.sex = sex;
}
else {
throw new InPutException("性别错误");
}
}
}
写个测试类去测试
public class Demo3 {
public static void main(String[] args){
Person person = new Person();
person.setAge(-1);
person.setSex('他');
}
}
结果
练习: 控制台版文件管理器
程序运行后显示所有的磁盘,如下所示:
1.c盘
2.e盘
3.e盘
4.f盘
请选择:
如果用户输入了1,回车后,则显示c盘下所有的目录和文件:
1.c:/a.txt(文件)
2.c:/programe file(目录)
3.返回上一层
请选择:
如果此时用户选择的是文件,例如输入1,回车,则显示文件的相关操作
1.查看文件信息
2.重命名文件
3.删除文件
4.移动文件
如果用户选择的是目录,则继续显示选择目录下的所有文件和目录,以此类推。
package com.day19;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Demo1 {
static Scanner sc= new Scanner(System.in);
public static void main(String[] args) {
show(null);
}
/**
* 显示目录所有文件和目录
* @param file
*/
public static void show(File file){
/*File[] files = File.listRoots();
//显示所有盘符
for (File files1: files) {
System.out.println(files1);
}*/
File[] files =null;
if (file==null){
files=File.listRoots();
}else {
files=file.listFiles();
}
//显示目录文件
System.out.println("0、返回上一层");
for (int i =0;i<files.length;i++) {
File file2 = files[i];
System.out.println((i+1)+"."+file2.getPath()+"("+(file2.isFile()?"文件":"目录")+")");
}
// System.out.println((files.length+1)+"返回上一层");
try {
while (true){
System.out.println("请选择");
int i=sc.nextInt();
if (i==0){
if (file==null){
show(null);
}else{
show(file.getParentFile());
}
}else {
File file2= files[i-1];
if (file2.isFile()){
//如果选择文件
showMenu(file2);
}else {
show(files[i-1]);
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("输入有误");
sc = new Scanner(System.in);
}
// if (i==0&&file==null){
// show(null);
// }else if (i==0 && file!=null){
// show(file.getParentFile());
// }else {
//
// }
}
private static void showMenu(File file){
System.out.println("1、查看文件 2、重命名 3、删除文件 4、移动文件 5、返回");
System.out.println("请选择:");
int i=sc.nextInt();
switch (i){
case 1:
System.out.println("***********文件详情************");
System.out.println("文件名:"+file.getName());
System.out.println("文路径:"+file.getPath());
System.out.println("文大小:"+file.length());
Date date =new Date(file.lastModified());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("文件修改时间:"+format.format(date));
showMenu(file);
break;
case 2:
System.out.println("当前文件名:"+file.getName());
System.out.println("请输入要修改的名称:");
String fileName = sc.next();
File file2 = new File(file.getParent() + "/" + fileName);
file.renameTo(file2);
showMenu(file2);
break;
case 3:
File file3 = file.getParentFile();
file.delete();
show(file3);
break;
case 4:
System.out.println("当前文件名:"+file.getPath());
System.out.println("请输入要移动到那个目录:");
String dir = sc.next();
File file4 = new File(dir);
if (!file4.exists()){
System.out.println("不存在");
}else {
file.renameTo(new File(file4.getPath()+"/"+file.getName()));
}
show(file.getParentFile());
break;
case 5:
show(file.getParentFile());
break;
default:
System.out.println("输入有误");
showMenu(file);
break;
}
}
}