throws is to throw an exception and subsequent code will not be executed. And try...catch throws the exception and continues to execute the following code.
package com.oracle; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Demo01Exception { /*Exception:编译期间异常,进行编译(写代码的过程) * runtimeException:运行期异常,java程序运行过程中出现的问题 *Error:错误(出现的错误无法调试,必须修改源代码) * */ public static void main(String[] args){ //*Exception:编译期间异常,进行编译(写代码的过程) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化日期对象。 Date date =null; try { date = sdf.parse("1999-0909"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }//把字符串格式的日期,解析为Date格式日期 System.out.println(date); System.out.println("kkkkk"); } }
Execution results: (Recommended learning: java video tutorial)
java.text.ParseException: Unparseable date: "1999-0909"(无法解释的错误。) at java.text.DateFormat.parse(DateFormat.java:357) at com.oracle.Demo01Exception.main(Demo01Exception.java:18) null kkkkk
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Demo01Exception { /*Exception:编译期间异常,进行编译(写代码的过程) * runtimeException:运行期异常,java程序运行过程中出现的问题 *Error:错误(出现的错误无法调试,必须修改源代码) * */ public static void main(String[] args) throws ParseException{ //*Exception:编译期间异常,进行编译(写代码的过程) SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化日期对象。 Date date =null; date = sdf.parse("1999-0909"); //把字符串格式的日期,解析为Date格式日期 System.out.println(date); System.out.println("kkkkk"); } }
Exception in thread "main" java.text.ParseException: Unparseable date: "1999-0909" at java.text.DateFormat.parse(DateFormat.java:357) at com.oracle.Demo01Exception.main(Demo01Exception.java:17)
Recommended related tutorials: java introductory tutorial
The above is the detailed content of What is the difference between throws and try...catch in java?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
