异常
异常是封装错误信息的对象,一般包含类型名称,提示信息和代码的错误行号。继承结构如下所示。
Throwable
|- Error 系统级错误
|- Exception 可修复的异常
|- 其他Exception
|- RuntimeExceptin
|- NullPointerException
|- ArrayIndexOutOfBoundsException
|- ArithmeticException
|- NumerFormatException
|- InputMismatchException
|- ClassCastException
异常一般使用try-catch-finally来捕捉。使用throws可抛出存在默认抛出管道的异常。throw可手动抛出异常。
public class Test1 { public static void main(String[] args) { System.out.println( "输入两个浮点数:"); double a = new Scanner(System.in).nextDouble(); double b = new Scanner(System.in).nextDouble(); try { double c = divide(a, b); System.out.println( a+"/"+b+"="+c); } catch(ArithmeticException e) { System.out.println( "除数不能为0"); System.out.println(e.getMessage()); //打印完整异常信息 e.printStackTrace(); } } private static double divide( double a, double b) { if(b == 0) { ArithmeticException e = new ArithmeticException( "/ by zero"); throw e; } double r = a / b; return r; } }
不能抛出的异常要包装成可以抛出的类型进行抛出。
public class Test1 { public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list, "2015-11-20", "2015-9-30", "2015-9-8", "2015-9-19", "2015-12-1", "2015-5-3", "kurgteurhtoehrotiheklrhtkeurh", "2015-5-19", "2015-5-8"); System.out.println(list); Collections.sort(list); System.out.println(list); System.out.println("---------------"); Collections.sort(list, new Comparator<String>() { @Override public int compare( String o1, String o2) { SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd"); try { Date d1 = fmt.parse(o1); Date d2 = fmt.parse(o2); return d1.compareTo(d2); } catch (Exception e) { //不是要修复异常 //而是包装成能抛出的异常,再抛出 throw new RuntimeException(e); } } }); System.out.println(list); } }
一般常见的类型有,NumberFormatException,ArrayIndexOutOfBoundsException,ParseException,IOException,ArithmeticException等等。用户除了系统定义的异常外也可以使用自定义异常。
public class Test1 { public static void main(String[] args) { System.out.println("用户名:"); String n = new Scanner(System.in).nextLine(); System.out.println("密码:"); String p = new Scanner(System.in).nextLine(); try { login(n, p); System.out.println("登陆成功"); } catch (UsernameNotFoundException e) { System.out.println("用户名错误"); } catch (WrongPasswordException e) { System.out.println("密码错误"); } } private static void login( String n, String p) throws UsernameNotFoundException, WrongPasswordException { if(! "abc".equals(n)) { throw new UsernameNotFoundException(); } if(! "123".equals(p)) { throw new WrongPasswordException(); } } }
public class UsernameNotFoundException extends Exception{ public UsernameNotFoundException() { super(); } public UsernameNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public UsernameNotFoundException(String message, Throwable cause) { super(message, cause); } public UsernameNotFoundException(String message) { super(message); } public UsernameNotFoundException(Throwable cause) { super(cause); } }
public class WrongPasswordException extends Exception{ public WrongPasswordException() { super(); } public WrongPasswordException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public WrongPasswordException(String message, Throwable cause) { super(message, cause); } public WrongPasswordException(String message) { super(message); } public WrongPasswordException(Throwable cause) { super(cause); } }

浙公网安备 33010602011771号