Java 19-6 throws way to handle exceptions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.