1. public class TestException {
    2. //来设计一个方法 测试自定义异常的使用
    3. public void testMyException() throws MyException{
    4. System.out.println("测试自定义异常的方法执行啦");
    5. if(3>2){//若满足某个条件
    6. throw new MyException("说明一下异常的具体问题");
    7. }
    8. }
    9. //设计一个方法
    10. public String testException() throws NullPointerException,StringIndexOutOfBoundsException,Exception{
    11. String str = "abc";
    12. str.length();
    13. str.charAt(10);
    14. // try{
    15. // System.out.println("try开始执行");
    16. // String str = null;
    17. // str.length();//空指针异常
    18. // System.out.println("try执行完毕");
    19. // return "try中的返回值";//事先约定好 返回值
    20. // }catch(Exception e){
    21. // //e.printStackTrace();//打印输出异常的名字
    22. // System.out.println("捕获到了异常");
    23. // }finally {
    24. // System.out.println("finally块执行啦");
    25. // }
    26. return "最终的返回值";
    27. }
    28. public static void main(String[] args){//JVM
    29. //System.out.println(10/0);
    30. TestException te = new TestException();
    31. try {
    32. te.testMyException();
    33. }catch (Exception e){
    34. e.printStackTrace();
    35. }
    36. //te.testException();
    37. // System.out.println("程序最早开始执行");
    38. // try {
    39. // System.out.println("try开始");
    40. // String str = "abc";
    41. // str.length();
    42. // str.charAt(10);//StringIndexOutOfBoundsException
    43. // System.out.println("try完毕");
    44. // }catch(NullPointerException e) {
    45. // System.out.println("捕获到了空指针异常");
    46. // }catch(Exception e){
    47. // System.out.println("捕获到了其他异常");
    48. // }finally {
    49. // System.out.println("我是finally执行块 我必须执行");
    50. // }
    51. // System.out.println("产生异常之后的所有程序");
    52. }
    53. }