What is the difference between throws and try...catch in java?

王林
Release: 2020-02-12 18:07:18
forward
2052 people have browsed it

What is the difference between throws and try...catch in java?

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");
	}
}
Copy after login

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
Copy after login
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");
	}
}
Copy after login
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)
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!