On the difference between abnormal structure chart, compile period anomaly and running period anomaly _java

Source: Internet
Author: User
Tags string format throw exception

Exception handling is typically 2 ways to either catch an exception try-catch or throw an exception throws

If a method is thrown behind a Run-time exception (throws RuntimeException), the caller does not need to handle

If a method throws a compile-time exception after it, the caller must handle it, or throw it or try-catch it;

Runtime exceptions are generally not processed, is generally a procedural logic error, such as the denominator of 0 as a divisor ...

Note If there is an exception in the try, the following statement is not executed, go back and look for catch matching exception handling, and the next statement will be processed (that is, the statement after try-catch-finally will continue to execute)

/*
Sometimes we can handle exceptions, but there are times when we simply don't have permission to handle an exception.
* Or, I can't handle it, I won't handle it.
* In order to solve the problem, Java in this case, provides another treatment scheme: throw.
*
Format
* Throws Exception class name
* Note: This format must be followed by the parentheses of the method.
*
Attention
* Try not to throw an exception on the main method.
* But I did it for the convenience of my lectures.
*
Summary
* Compile-time exception thrown, the future caller must handle.
* Runtime exception thrown, future calls can not be processed.
*/

General RuntimeException and its subclasses do not handle the exception (do not throw nor capture), if you really know that the runtime will be thrown, then directly check the logic of modifying the program is ok!!!

Package anomaly;

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.Date;

public class Exceptiondemo {public
	static void Main (string[] args) {
		System.out.println (before calling method);
		try {
			method ();//compile-time exception throw must handle
		} catch (ParseException e) {
			e.printstacktrace ();
		}
		System.out.println ("Invoke method two after calling Method 1");
		Method2 ();
	}

	public static void Method2 () throws runtimeexception{
		int a = ten;
		int b = 0;
		System.out.println ("a/b=" +a/b);//Run-time exceptions can be handled without processing, and if handled to throw an exception throws, then the caller does not have to handle the exception
	} public

	static void Method () throws ParseException {
		string string = "2015-05-30";
		SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");//will step up to match string format, not throw exception
		SimpleDateFormat sdf2 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); If the string is only 2015-05-30 and has no back, it runs an exception because the SDF2 cannot match HH:mm:ss
		Date date = Sdf.parse (string);
		SYSTEM.OUT.PRINTLN ("Date formatted:" + date);
	}


Exception Structure Chart:

Error exceptions we programmers don't have to deal with it,

RuntimeException runtime exception We don't have to deal with that.

The other is the compile-time anomaly that we're going to deal with.

The above is a small series for everyone to talk about the abnormal structure diagram, compile period anomaly and the difference of the running period of the whole content of the differences, hope to help everyone, a lot of support cloud Habitat Community ~

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.