1 /*2 Sometimes we can handle exceptions, but sometimes we don't have the right to handle an exception at all. 3 or, I can't handle it, I won't handle it. 4 in order to solve the problem of error, Java provides another way to deal with this situation: throw . 5 6 format:7 throws Exception class Name 1, exception class name 2,...8 NOTE: This format must be followed by the parentheses of the method. 9 Ten Note: One try not to throw an exception on the main method. A but I did it for the convenience of my lectures. - (The following exception jumps to the page) - the Summary: - the compile-time exception is thrown and must be handled by the caller in the future. - the run-time exception is thrown, and future calls are not processed. - */ + Importjava.text.ParseException; - ImportJava.text.SimpleDateFormat; + Importjava.util.Date; A Public classExceptiondemo { at Public Static voidMain (string[] args) { -SYSTEM.OUT.PRINTLN ("The weather is fine today"); - Try { - method (); -}Catch(ParseException e) { - e.printstacktrace (); in } -System.out.println ("But there should be no fog and haze"); to + method2 (); - } the * //run-time exception throws $ Public Static voidMETHOD2 ()throwsArithmeticException {Panax Notoginseng intA = 10; - intb = 0; theSystem.out.println (A/b); + } A the //throwing exceptions at compile time + //thrown on the method declaration, is to tell the caller that you are aware that I have a problem. - Public Static voidMethod ()throwsParseException { $String s = "2014-11-20"; $SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); -Date d =Sdf.parse (s); - System.out.println (d); the } -}
Java 19-6 throws way to handle exceptions