A:异常的概述java
B:异常的分类程序员
Error面试
C:异常的继承体系算法
* Throwable * Error * Exception * RuntimeException
A:JVM默认是如何处理异常的数据库
B:案例演示windows
public class Demo1_Exception { public static void main(String[] args) { // demo1(); Demo d = new Demo(); int x = d.div(10, 0); //new ArithmeticException(/ by zero) 除数是0,违反算数运算法则 System.out.println(x); } private static void demo1() { int[] arr = {11,22,33,44,55}; // arr = null; //NullPointerException 空指针异常 System.out.println(arr[10]); //ArrayIndexOutOfBoundsException 数组索引越界异常 } } class Demo{ public int div(int a,int b) { return a / b; } }
A:异常处理的两种方式数组
a:try…catch…finally服务器
B:try...catch处理异常的基本格式eclipse
C:案例演示jvm
public class Demo2_Exception { /* try:用来检测异常 catch:用来捕获异常的 finally:释放资源 当经过trycatch将异常处理,程序继续向下执行*/ public static void main(String[] args) { Demo2 d = new Demo2(); try { int x = d.div(10, 0); System.out.println(x); }catch(ArithmeticException a) { System.out.println("出错了,除数为0了"); } System.out.println("----------"); } } class Demo2{ public int div(int a,int b) { return a / b; } }
A:案例演示
public class Demo3_Exception { //安卓:客户端开发,如何处理异常?try{}catch(Exception e) {} //JavaEE:服务端开发,通常都是底层开发,从底层向上抛 public static void main(String[] args) { // demo1(); int a = 10; int b = 0; int[] arr = {11,22,33,44,55}; //JdK7如何处理多个异常 try { System.out.println(a / b); System.out.println(arr[10]); } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) { System.out.println("出错了"); } } private static void demo1() { int a = 10; int b = 0; int[] arr = {11,22,33,44,55}; try { System.out.println(a / b); System.out.println(arr[10]); arr = null; System.out.println(arr[0]); } catch (ArithmeticException e) { System.out.println("除数不能为零"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("索引越界了"); } catch (Exception e) { //Exception e = new NullPointerException(); System.out.println("出错了"); } System.out.println("over"); } }
A:编译期异常和运行期异常的区别
编译时异常
运行时异常
B:案例演示
import java.io.FileInputStream; public class Demo4_Exception { /* 编译时异常:也叫健壮性异常,不处理编译通不过。 运行时异常:就是程序员所犯的错误,须要回来修改代码。 */ public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("xxx.txt"); } catch (Exception e) { } } }
A:Throwable的几个常见方法
a:getMessage()
b:toString()
c:printStackTrace()
B:案例演示
public class Demo5_throwable { public static void main(String[] args) { try { System.out.println(1/0); } catch (Exception e) { //Exception e = new ArithmeticException(" / by zero"); // System.out.println(e.getMessage()); //获取异常信息 System.out.println(e); //调用toString方法,打印异常类名和异常信息 e.printStackTrace(); //jvm默认就用这种方式处理异常 } } }
A:throws的方式处理异常
B:案例演示
public class Demo6_Exception {
//编译时异常的抛出必须对其进行处理
//运行时异常的抛出能够处理也能够不处理
public static void main(String[] args) throws Exception { Person p = new Person(); p.setAge(-17); System.out.println(p.getAge()); } } class Person{ private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) throws RuntimeException { if(age > 0 && age <= 150) { this.age = age; }else { throw new RuntimeException("年龄非法"); // System.out.println("请回火星吧"); } } }
A:throw的概述
B:案例演示
C:throws和throw的区别
a:throws
b:throw
A:finally的特色
B:finally的做用
C:案例演示
public static void main(String[] args) { try { System.out.println(10/0); } catch (Exception e) { System.out.println("除数为零了"); System.exit(0); //退出jvm虚拟机 return; } finally { System.out.println("看看我执行了吗"); } }
A:面试题1
final,finally和finalize的区别
B:面试题2
public class Demo8_test { public static void main(String[] args) { Demo3 d = new Demo3(); System.out.println(d.method()); } } class Demo3 { public int method() { int x = 10; try { x = 20; System.out.println(1/0); return x; } catch (Exception e) { x = 30; return x; } finally { x = 40; } } }
A:为何须要自定义异常
B:自定义异常概述
C:案例演示
class AgeOutOfBoundsException extends RuntimeException { public AgeOutOfBoundsException() { super(); } public AgeOutOfBoundsException(String message) { super(message); } }
public void setAge(int age) throws AgeOutOfBoundsException { if(age > 0 && age <= 150) { this.age = age; }else { throw new AgeOutOfBoundsException("年齡非法"); } }
A:异常注意事项
B:如何使用异常处理
区别:
键盘录入一个int类型的整数,对其求二进制表现形式
import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class Demo10_test { /** 分析 * 1.建立键盘录入对象 * 2.将键盘录入的结果存储在String类型的字符串中,存储int类型中,若是有不符合条件的直接报错,没法进行后续判断 * 3.是键盘录入的结果转换成int类型的数据,是正确的仍是错误的。 * 4.正确的直接转换 * 5.错误的要对应的判断 * */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); while(true) { String line = sc.nextLine(); //将键盘录入的结果存储在line中 try { int num = Integer.parseInt(line); //将字符串转换为整数 System.out.println(Integer.toBinaryString(num));//将整数转换为二进制 break; //跳出循环 } catch (Exception e) { try { new BigInteger(line); System.out.println("录入错误,您录入的是一个过大的整数,请从新输入:"); } catch (Exception e2) { //Alt + Shift + z try { new BigDecimal(line); System.out.println("录入错误,您录入的是一个小数,请从新输入:"); } catch (Exception e3) { System.out.println("录入错误,您录入的是非法字符,请从新输入:"); } } } } // BigInteger big = new BigInteger("123"); } }
A:File类的概述
File更应该叫作一个路径
B:构造方法
C:案例演示
import java.io.File; public class Demo1_File { public static void main(String[] args) { // demo1(); // demo2(); File parent = new File("C:\\Users\\albert\\Desktop\\JavaGuide-master\\Java相关"); String child = "ArrayList.md"; File file = new File(parent, child); System.out.println(file.exists()); System.out.println(parent.exists()); } private static void demo2() { String parent = "C:\\Users\\albert\\Desktop\\JavaGuide-master\\Java相关"; String child = "ArrayList.md"; File file = new File(parent,child); System.out.println(file.exists()); } private static void demo1() { File file = new File("C:\\Users\\albert\\Desktop\\JavaGuide-master\\Java相关"); System.out.println(file.exists()); File file2 = new File("xxx.txt"); System.out.println(file2.exists()); } }
A:建立功能
B:案例演示
注意事项:
import java.io.File; import java.io.IOException; public class Demo2_FileMethod { public static void main(String[] args) throws IOException { // demo1(); File dir1 = new File("aaa"); System.out.println(dir1.mkdir()); File dir2 = new File("bbb.txt"); System.out.println(dir2.mkdir()); File dir3 = new File("ccc\\ddd"); System.out.println(dir3.mkdirs()); //建立多级目录 } private static void demo1() throws IOException { File file = new File("yyy.txt"); //建立文件 System.out.println(file.createNewFile()); //若是没哟就建立,返回true File file2 = new File("zzz"); System.out.println(file2.createNewFile()); } }
A:重命名和删除功能
B:重命名注意事项
C:删除注意事项:
import java.io.File; public class Demo3_FileMethod { public static void main(String[] args) { // demo1(); File file1 = new File("yyy.txt"); System.out.println(file1.delete()); File file2 = new File("aaa"); System.out.println(file2.delete()); File file3 = new File("ccc"); //被删除的文件必须为空 System.out.println(file3.delete()); } private static void demo1() { File file1 = new File("ooo.txt"); File file2 = new File("D:\\xxx.txt"); System.out.println(file1.renameTo(file2)); } }
A:判断功能
B:案例演示
import java.io.File; public class Demo4_FileMethod { public static void main(String[] args) { // demo1(); File file = new File("zzz"); file.setReadable(false); //windows认为全部的文件都是可读的 System.out.println(file.canRead()); file.setWritable(true); System.out.println(file.canWrite()); //windows系统能够设置为不可写 File file2 = new File("lalala.txt"); System.out.println(file2.isHidden()); //判断是不是隐藏文件 } private static void demo1() { File dir1 = new File("ccc"); System.out.println(dir1.isDirectory()); //判断是不是文件夹 File dir2 = new File("zzz"); System.out.println(dir2.isDirectory()); System.out.println(dir1.isFile()); //判断是不是文件 System.out.println(dir2.isFile()); } }
A:获取功能
B:案例演示
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; public class Demo5_FileMethod { public static void main(String[] args) { // demo1(); File dir = new File("E:\\Javawork\\JavaSE_File"); String[] arr = dir.list(); //仅为了获取文件名 for (String string : arr) { System.out.println(string); } File[] subFiles = dir.listFiles(); for (File file : subFiles) { //获取文件对象 System.out.println(file); } } private static void demo1() { File file1 = new File("ccc.txt"); File file2 = new File("E:\\Javawork\\JavaSE_File\\ccc.txt"); // System.out.println(file1.getAbsolutePath()); //获取绝对路径 // System.out.println(file2.getAbsolutePath()); // System.out.println(file1.getPath()); //获取构造方法中传入的路径 // System.out.println(file2.getPath()); // System.out.println(file1.getName()); // System.out.println(file2.getName()); //获取文件或文件夹的名称 // System.out.println(file1.length()); // System.out.println(file2.length()); Date d = new Date(file1.lastModified()); //文件的最后修改时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); System.out.println(sdf.format(d)); System.out.println(file2.lastModified()); } }
A:案例演示
import java.io.File; public class Demo6_test { /** A:案例演示 * 需求:判断E盘目录下是否有后缀名为.jpg的文件,若是有,就输出该文件名称*/ public static void main(String[] args) { File dir = new File("E:\\"); /*String[] arr = dir.list(); //获取E盘下全部文件及文件夹 for (String string : arr) { if(string.endsWith(".jpg")) { System.out.println(string); } }*/ File[] subFiles = dir.listFiles(); //获取E盘下全部的文件和文件夹对象 for (File subFile : subFiles) { if(subFile.isFile() && subFile.getName().endsWith(".jpg")) { System.out.println(subFile); } } } }
A:文件名称过滤器的概述
B:文件名称过滤器的使用
C:源码分析
File dir = new File("E:\\"); String[] arr = dir.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { // System.out.println(dir); // System.out.println(name); File file = new File(dir, name); return file.isFile() && file.getName().endsWith(".jpg"); } }); for (String string : arr) { System.out.println(string); }