Java exception handling hands-on brain problems

Source: Internet
Author: User
Tags getmessage try catch

Hands on the brain 01

SOURCE program:

Import javax.swing.*;
Class Aboutexception {
public static void Main (string[] a)
{
int I=1, j=0, K;
k=i/j;
Try
{
K = i/j; Causes Division-by-zero exception
throw new Exception ("hello.exception!");
}
catch (ArithmeticException e)
{
System.out.println ("was removed by 0." + e.getmessage ());
}
catch (Exception e)
{
if (e instanceof arithmeticexception)
System.out.println ("by 0 divide");
Else
{
System.out.println (E.getmessage ());

}
}
Finally
{
Joptionpane.showconfirmdialog (null, "OK");
}
}
}

Program:

Hands-on brain: Multilayer anomaly capture-1

SOURCE program:

public class Catchwho {
public static void Main (string[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/Inner layer Try-catch");
}

throw new ArithmeticException ();
}
catch (ArithmeticException e) {
System.out.println ("occurring arithmeticexception");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/outer try-catch");
}
}
}

Program:

Hands-on brain: Multilayer anomaly capture-2

SOURCE program:

public class CatchWho2 {
public static void Main (string[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException ();
}
catch (ArithmeticException e) {
System.out.println ("arrayindexoutofboundsexception" + "/Inner layer Try-catch");
}
throw new ArithmeticException ();
}
catch (ArithmeticException e) {
System.out.println ("occurring arithmeticexception");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/outer try-catch");
}
}
}

Program:

Hands on the brain-----

When there are multiple nested try...catch....finally, pay special attention to finally the timing of execution

code example:

Execution Result:

Attention:

When there are multiple layers of nested Finally, exceptions are thrown at different levels and thrown at different locations, which may result in a different order of finally statement execution

Hands on the brain--------

Discrimination: Finally statement will be 1 execution?

Example code:

Execution Result:

Summarize:
(1) The Try statement is not executed, such as return before the try statement, so that the finally statement is not executed. This also shows that the necessary rather than sufficient condition for the finally statement to be executed is that the corresponding try statement must be executed.

(2) There is system.exit (0) in the try block |catch block, such a statement. System.exit (0) Terminates the Java Virtual Machine JVM, and even the JVM is stopped, all of it is over, and the finally statement is not executed.

In Try-catch-finally, when return encounters Finally,return is invalid for Finally, namely:

1. When you return in the try Catch block, finally is executed.

The return statement in 2.finally will overwrite the return statement effect in the try catch block.

Conclusion: The return statement is not necessarily the exit of the function, but when the return is executed, only the value after return is copied to the return value variable. (Part of the summary is from the network)

Verify:

Not compiled by graphs

But after fixing the code, we compile it.

Why?

The throws statement indicates that a method may have an exception that cannot be handled by itself and needs to be handled by the caller.

Java exception handling hands-on brain problems

Related Article

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.