|
@@ -0,0 +1,127 @@
|
|
|
+package com.sf.day11;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.security.PublicKey;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.DriverManager;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+public class Test01 {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 错误 Error
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void test1(){
|
|
|
+ recursion();
|
|
|
+ }
|
|
|
+ public void recursion(){
|
|
|
+ recursion();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test2(){
|
|
|
+ int[] arr = new int[Integer.MAX_VALUE];
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test3(){
|
|
|
+ // OutOfMemoryError
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ while (true){
|
|
|
+ stringBuilder.append("sf");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 运行时异常 RunTimeException
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t1(){
|
|
|
+ int[][] arr = new int[3][];
|
|
|
+ System.out.println(arr[0].length);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t2(){
|
|
|
+ Object obj = 3;
|
|
|
+ String str = (String) obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t3(){
|
|
|
+ int[] arr = new int[5];
|
|
|
+ for (int i= 0;i <=5 ;i++){
|
|
|
+ System.out.println(arr[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t4(){
|
|
|
+ //InputMismatchException
|
|
|
+ Scanner input = new Scanner(System.in);
|
|
|
+ System.out.print("请输入一个整数:");//输入非整数
|
|
|
+ int num = input.nextInt();
|
|
|
+ input.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t5(){
|
|
|
+ int a = 1;
|
|
|
+ int b = 0;
|
|
|
+ //ArithmeticException
|
|
|
+ System.out.println(a/ b);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编译时异常(受检查异常)
|
|
|
+ * 声明异常 在方法后+ throws 异常类名 , 异常类型2.。。。。
|
|
|
+ */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t6() throws InterruptedException { //声明
|
|
|
+ Thread.sleep(1000); //多线程这块 //休眠1000毫秒
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t7() throws ClassNotFoundException {
|
|
|
+ Class<?> aClass = Class.forName("java.lang.String");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t8() throws SQLException {
|
|
|
+ Connection connection = DriverManager.getConnection(".....");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t9() throws FileNotFoundException {
|
|
|
+ FileInputStream fileInputStream = new FileInputStream("java.txt");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t10() throws RuntimeException{
|
|
|
+ int a = 0; int b = 1;
|
|
|
+ if(a == b){
|
|
|
+ throw new RuntimeException("a和b不能相等");
|
|
|
+ }
|
|
|
+ System.out.println("==============");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|