1.
public class Parentchildtest {
public static void Main (string[] args) {
Parent parent=new parent ();
Parent.printvalue ();
Child child=new Child ();
Child.printvalue ();
Parent=child;
Parent.printvalue ();
parent.myvalue++;
Parent.printvalue ();
((child) parent). myvalue++;
Parent.printvalue ();
}
}
Class parent{
public int myvalue=100;
public void Printvalue () {
System.out.println ("Parent.printvalue (), myvalue=" +myvalue);
}
}
Class Child extends parent{
public int myvalue=200;
public void Printvalue () {
System.out.println ("Child.printvalue (), myvalue=" +myvalue);
}
}
Experiment:
Summary: When the parent class has the same method as the child class, the child class method is called when the subclass object calls the parent class method.
2.CathWho
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");
}
}
}
·:
3. CathWho2
public class CatchWho2 {
public static void Main (string[] args) {
try {
try {
throw new ArithmeticException ();
}
catch (ArithmeticException e) {
System.out.println ("occurring arithmeticexception");
}
throw new ArrayIndexOutOfBoundsException ();
}
catch (ArithmeticException e) {
System.out.println ("arrayindexoutofboundsexception" + "/Inner layer Try-catch");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/outer try-catch");
}
}
}
4. Embededfinally
public class Embededfinally {
public static void Main (String args[]) {
int result;
try {
System.out.println ("in Level 1");
try {
System.out.println ("in Level 2");
result=100/0; Level 2
try {
System.out.println ("in 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");
}
}
}
Experiment:
Summary: Drop a package and use catch to capture that package. Packages of different class names must be caught with a catch of the same type as the package.
5. Systemexitandfinally.java
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");
}
}
}
Experiment:
Summary: The finally statement is bound to execute.
6. Printexpressionstack.java
Usingexceptions.java
Demonstrating the GetMessage and Printstacktrace
Methods inherited into all exception classes.
public class Printexceptionstack {
public static void Main (String args[])
{
try {
Method1 ();
}
catch (Exception e) {
System.err.println (E.getmessage () + "\ n");
E.printstacktrace ();
}
}
public static void Method1 () throws Exception
{
Method2 ();
}
public static void Method2 () throws Exception
{
Method3 ();
}
public static void Method3 () throws Exception
{
throw new Exception ("Exception thrown in method3");
}
}
:
Multi-state and abnormal handling after-class exercises