时间:2022-12-29 10:52:25 | 栏目:JAVA代码 | 点击:次
NullPointerException
ClassCastException
ArrayIndexOutOfBoundsException
ArithmeticExecption
NegativeArrayException
格式:
try { 可能出现问题的代码; }catch(异常名 变量) { 针对问题的处理; }finally { 释放资源; }
代码:
package yichang01; public class ExceptionDemo { public static void main(String[] args){ System.out.println("start"); int[] arr = {1,2,3,4,5}; try { System.out.println(arr[5]); }catch (Exception e){ System.out.println("发生了异常:"+e.getMessage()); e.printStackTrace();//打印异常的栈 }finally {//有没有异常都会执行 finally 里的代码 System.out.println("释放资源,必须执行的代码块"); } //System.out.println(arr[5]);//数组下标越界 System.out.println("end"); } }
变形格式:
try { 可能出现问题的代码; }catch(异常名 变量) { 针对问题的处理; }
package yichang01; public class ExceptionDemo { public static void main(String[] args) { System.out.println("start"); int a = 10; int b = 0; try { System.out.println(a / b); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("end"); } }
注意:
try
里面的代码越少越好catch
里面必须有内容,哪怕给出一个简单的提示package yichang01; public class ExceptionDemo { public static void main(String[] args) { //method1();//一个异常的情况 //method2();//两个异常、分别处理的情况 //method3();//两个异常、一块处理的情况 method4(); } private static void method4() { int a = 10; int b = 0; int[] arr = {1, 2, 3}; try { Object o = 123; String s = (String) o; //ClassCastException System.out.println(arr[3]); //IndexOutOfBoundException System.out.println(a / b); //ArithmeticException System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?"); } catch (ArithmeticException e) { System.out.println("除数不能为0"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("你访问了不该的访问的索引"); } catch (Exception e) { System.out.println("出问题了"); } System.out.println("over"); } private static void method3() { int a = 10; int b = 0; int[] arr = {1, 2, 3, 4, 5}; try { System.out.println(a / b); System.out.println(arr[5]); } catch (ArithmeticException e) { System.out.println("除数不能为0"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("索引越界"); } System.out.println("end"); } private static void method2() { int a = 10; int b = 0; try { System.out.println(a / b); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("end1"); int[] arr = {1, 2, 3, 4}; try { System.out.println(arr[4]); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("end2"); } private static void method1() { int a = 10; int b = 0; try { System.out.println(a / b); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } System.out.println("end"); } }
获取异常信息,返回字符串。
获取异常类名和异常信息,返回字符串。
获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
通常用该方法将异常内容保存在日志文件中,以便查阅。
package yichang01; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ExceptionDemo { public static void main(String[] args) { String s = "2019-06-17"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date d = sdf.parse(s); System.out.println(d); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println(e.toString()); e.printStackTrace(); } System.out.println("end"); } }
定义功能方法时,需要把出现的问题暴露出来,让调用者去处理
package yichang02; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ExceptionDemo { public static void main(String[] args) throws ParseException { System.out.println("start"); method1(); System.out.println("end11"); //method2(); System.out.println("end22"); } private static void method2() throws ArithmeticException {//运行期异常抛出 int a = 10; int b = 0; System.out.println(a / b); } private static void method1() throws ParseException {//编译期异常抛出 String s = "2014-11-20"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = sdf.parse(s); System.out.println(d); } }
在功能方法内部出现某种情况,程序不能继续运行,需要进行跳转时,用 throw 把异常对象抛出
package yichang02; public class ExceptionDemo { public static void main(String[] args) { Student s = new Student(); s.setAge(-30); } } class Student { private int age; public int getAge() { return age; } public void setAge(int age) { if (age > 1) { this.age = age; } else { try { throw new Exception("年龄不能小于1");//throw:方法内部,一般出现了逻辑错误,手动抛出的自定义异常 } catch (Exception e) { e.printStackTrace(); } this.age = 1; } } }
throws
throw
package yichang02; class ExceptionDemo { public static void main(String[] args) { //method1(); try { method2(); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } } private static void method2() throws Exception { int a = 10; int b = 0; if (b == 0) { throw new Exception(); } else { System.out.println(a / b); } } private static void method1() { int a = 10; int b = 0; if (b == 0) { throw new ArithmeticException(); } else { System.out.println(a / b); } } }
try...catch...finally try...catch try...catch...catch... try...catch...catch...finally try...finally
被finally控制的语句体一定会执行
用于释放资源,在IO流操作和数据库操作中会见到
package yichang02; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; class ExceptionDemo { public static void main(String[] args) { String s = "2019-6-17 12:12:12"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d = null; try { d = sdf.parse(s); } catch (ParseException e) { e.printStackTrace(); System.exit(0); } finally { System.out.println("这是必然执行的代码"); } System.out.println(d); } }
package yichang02; class ExceptionDemo { public static void main(String[] args) { System.out.println(getInt()); } public static int getInt() { int a = 10; try { System.out.println(a / 0); a = 20; return a; } catch (Exception e) { System.out.println("catch"); a = 30; return a; } finally {//释放资源,会执行的代码 System.out.println("finally"); a = 40; return a; } // return a; } }
1、final
:最终的意思,可以修饰类,成员变量,成员方法
2、finally
:是异常处理的一部分,用于释放资源。
3、finalize
:是Object类的一个方法,用于垃圾回收