Polymorphism and anomaly------hands-on brain

Source: Internet
Author: User

Classroom hands-on brain 1. Does the following statement cause a compilation error?

Class mammal{}

Class Dog extends Mammal {}

Class Cat extends mammal{}

public class Testcast

{

public static void Main (String args[])

{

Mammal m;

Dog D=new Dog ();

Cat c=new Cat ();

m=d;//does not give an error, the object of the subclass can be assigned a reference to the parent class

d=m;//will give an error, the reference to the parent class cannot be assigned directly to the object of the child class. To assign a value, you must force the type conversion

D= (DOG) m;//no error, forced type conversion

d=c;//will error, although these two classes derive from the same class, but there is no inheritance between the two classes, each other belongs to a different class, cannot be directly assigned to the value

C= (Cat) m;//does not error, forced type conversion

D = (Dog) c;//This sentence is my own attached, this sentence will also be an error. Because coercion of type conversions is not possible between different classes

}

}

2CatchWho

Package Seven;

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 ("occurs ArithmeticException");

}

Catch (ArrayIndexOutOfBoundsException e)

{

System. out. println ("ArrayIndexOutOfBoundsException"

+ "/outer try-catch");

}

}

}

3catchwho2

Package Seven;

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 ("occurs ArithmeticException");

}

Catch (ArrayIndexOutOfBoundsException e)

{

System. out. println ("arrayindexoutofboundsexception" + "/outer try-catch");

}

}

}

In contrast to the two examples of CATCHWH and CatchWho2, we can conclude that when a try statement is nested, if the exception thrown by the inner try statement can be disposed of by the inner catch statement, it is handled by a try statement inside the layer, However, the outer try statement is also executed because a child inside the outer try statement block executes quickly. If the catch statement of the inner try statement does not handle the exception successfully, the exception is thrown to the upper-level try statement. Also, once an exception is thrown inside a try statement block, the statement after the statement that throws the exception is not executed.

4 aboutexception

Package Seven;

import javax.swing.*;

Public 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 ("removed by 0");

Else

{

System. out. println (E.getmessage ());

}

}

finally

{

Joptionpane. Showconfirmdialog (null, "OK");

}

}

}

Because k=i/j, the statement is not inside a try statement block, so the catch statement block cannot handle the exception.

5 embededfinally

Package Seven;

Public class embededfinally {

Public Static void Main (String args[]) {

int result;

Try {

System. out. println ("at level 1");

Try {

System. out. println ("at Level 2");

result=100/0; Level 2

Try {

System. out. println ("at Level 3");

result=100/0; Level 3

}

Catch (Exception e) {

System. out. println ("Level 3:" + E.getclass (). toString ());

}

finally {

System. out. println ("at level 3 finally");

}

result=100/0; Level 2

}

Catch (Exception e) {

System. out. println ("Level 2:" + E.getclass (). toString ());

}

finally {

System. out. println ("at level 2 finally");

}

result = 100/0; Level 1

}

Catch (Exception e) {

System. out. println ("Level 1:" + e.getclass (). toString ());

}

finally {

System. out. println ("at level 1 finally");

}

}

}

Called from the inner layer to the outer layer.

Is the 6 finally statement block bound to be executed?

Package Seven;

Public class systemexitandfinally {

Public Static void Main (string[] args)

{

Try {

System. out. println ("in Main");

Throw New Exception ("Exception is thrown in main");

System.exit (0);

}

Catch (Exception e)

{

System. out. println (E.getmessage ());

System. Exit (0);

}

finally

{

System. out. println ("in finally");

}

}

}

Not necessarily. In general, the Finallya statement block can be executed only if the TRY statement block corresponding to the fianally statement block is executed. But this is not absolute, as in this example, even if the try statement block corresponding to the finally statement block is executed, the finally statement block is still not executed. Because there are statements in the CATCH statement block that force the program to end.

Polymorphism and anomaly------hands-on brain

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.