What is the difference between runtime exception and checkedexception?Java provides two main types of exceptions: Runtime exception and checkedexception. All checked exception are derived from the Java.lang.Exception class, while RuntimeException is from Java.lang. Derived from the RuntimeException or Java.lang.Error class.
Their differences are manifested in two ways: both institutional and logical.
First, the mechanism
They differ in mechanism in two points: 1. How to define a method; 2. How to handle thrown exceptions. Consider the following definition of checkedexception:
public class Checkedexception extends Exception
{
Public checkedexception () {}
Public checkedexception (String message)
{
Super (message);
}
}
And an example of using exception:
public class Exceptionalclass
{
public void Method1 ()
Throws Checkedexception
{
... throw new Checkedexception ("... Error ");
}
public void Method2 (String arg)
{
if (arg = = null)
{
throw new NullPointerException ("Method2 parameter arg is null!");
}
}
public void Method3 () throws Checkedexception
{
Method1 ();
}
}
As you may have noticed, two methods method1 () and METHOD2 () will throw exception, but only method1 () is declared. In addition, METHOD3 () itself does not throw exception, but it declares that it will throw checkedexception. Before explaining to you, let's take a look at the main () method of this class:
public static void Main (string[] args)
{
exceptionalclass example = new Exceptionalclass ();
Try
{
Example.method1 ();
Example.method3 ();
}
catch (Checkedexception ex) {} example.method2 (null);
}
In the main () method, if you want to call METHOD1 (), you have to put the call in the Try/catch block because it throws checkedexception.
By contrast, when you call METHOD2 (), you don't need to put it in the Try/catch block because it throws exception not checkedexception, but runtime exception. A method that throws RuntimeException does not have to be declared when it is defined to throw exception.
Now, let's take a look at method3 (). It calls Method1 () and does not place the call in the Try/catch program block. It is by declaring that it will throw method1 () will throw the exception to avoid doing so. It does not capture this exception, but passes it on. In fact, the main () method can do the same, by declaring that it throws checkedexception to avoid the use of Try/catch blocks (which, of course, we oppose).
Summary:
* Runtime Exceptions:
No declaration is required when defining a method to throw runtime exception;
You do not need to capture this runtime exception when calling this method;
RuntimeException is derived from the Java.lang.RuntimeException or Java.lang.Error class.
* Checked Exceptions:
When defining a method, you must declare all checked exception that may be thrown;
When calling this method, it must catch its checked exception, or it will have to pass its exception;
Checked exception is derived from the Java.lang.Exception class.
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.