import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Test3 { public static void main(String[] args) { //异常分类 1.编译期 2.运行期 // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date parse = null; try { parse = simpleDateFormat.parse("2021-3-3"); //parse 需要捕捉,不然编译报错 } catch (ParseException e) { e.printStackTrace(); } System.out.println(parse); int i = 1 / 0; // by zero int[] arr = {1, 2, 3,}; System.out.println(arr[3]);//下标:0 1 2,无3 System.out.println("hello,world"); } }
public class Test4 { public static void main(String[] args) { //自定义异常,这只是个例子 try { TestE testE = new TestE(); testE.do1(); } catch (MyExcetion e) { e.printStackTrace(); } System.out.println("hello,world"); } public static class TestE { public void do1() throws MyExcetion { throw new MyExcetion("123"); } } public static class MyExcetion extends Exception { public MyExcetion() { super(); } public MyExcetion(String msg) { super("MyExcetion:" + msg); } } }