1怎样判断对象是否可以转换?
可以使用instanceof运算符判断一个对象是否可以转换为指定的类型
实验代码:
public class TestInstanceof
{ public static void main(String[] args) { //声明hello时使用Object类,则hello的编译类型是Object,Object是所有类的父类 //但hello变量的实际类型是String Object hello = "Hello"; //String是Object类的子类,所以返回true。 System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object)); //返回true。 System.out.println("字符串是否是String类的实例:" + (hello instanceof String)); //返回false。 System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math)); //String实现了Comparable接口,所以返回true。 System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable)); String a = "Hello"; //String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过 //System.out.println("字符串是否是Math类的实例:" + (a instanceof Math)); }}实验结果:
字符串是否是Object类的实例:true
字符串是否是String类的实例:true字符串是否是Math类的实例:false字符串是否是Comparable接口的实例:true2 请看以下“变态”的类
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); }}
运行结果:
Parent.printValue(),myValue=100
Child.printValue(),myValue=200Child.printValue(),myValue=200Child.printValue(),myValue=200Child.printValue(),myValue=201为啥呢?!
原因: 当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。 这个特性实际上就是面向对象“多态”特性的具体表现。
如果子类与父类有相同的字段,则子类中的字段会代替或隐藏父类的字段,子类方法中访问的是子类中的字段(而不是父类中的字段)。如果子类方法确实想访问父类中被隐藏的同名字段,可以用super关键字来访问它。 如果子类被当作父类使用,则通过子类访问的字段是父类的!
3 代码:
public class CatchWho {
public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("发生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); } } }运行结果:
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException
4
当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。 请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。 特别注意: 当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
EmbedFinally.java源代码:
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("In Level 3 finally"); } // result=100/0; //Level 2
} catch (Exception e) { System.out.println("Level 2:" + e.getClass().toString()); } finally { System.out.println("In Level 2 finally"); } // result = 100 / 0; //level 1 } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { System.out.println("In Level 1 finally"); } }
}
运行结果:
in Level 1
in Level 2in Level 3Level 3:class java.lang.ArithmeticExceptionIn Level 3 finallyIn Level 2 finallyIn Level 1 finally5
当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。 可使用printStackTrace 和 getMessage方法了解异常发生的情况: printStackTrace:打印方法调用堆栈。 每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。 请通过 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" ); }}运行结果:
温馨提示: Exception thrown in method3
java.lang.Exception: Exception thrown in method3// System.err.println( "温馨提示: "+e.getMessage() + "\n " ); at PrintExceptionStack.method3(PrintExceptionStack.java:28) at PrintExceptionStack.method2(PrintExceptionStack.java:23) at PrintExceptionStack.method1(PrintExceptionStack.java:18) at PrintExceptionStack.main(PrintExceptionStack.java:8)
e.printStackTrace();//打印错误栈里的内容
6
一个方法可以声明抛出多个异常 int g(float h) throws OneException,TwoException { …… } ThrowMultiExceptionsDemo.java示例展示了相关特性。 注意一个Java异常处理中的一个比较独特的地方:
ThrowMultiExceptionsDemo.java代码:
import java.io.*;
public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉异常"); } }private static void throwsTest() throws ArithmeticException,IOException {
System.out.println("这只是一个测试"); // 程序处理过程假设发生异常 throw new IOException(); //throw new ArithmeticException(); } }运行结果:
这只是一个测试
捕捉异常7
在实际开发中,经常需要将特定的“过于专业”的异常转换为一个“业务”异常,然后在调用者处进行捕获与处理。 ExceptionLinkInRealWorld.java示例展示了典型的异常处理代码模板
ExceptionLinkInRealWorld.java示例:
/**
* 自定义的异常类 * @author JinXuLiang * */class MyException extends Exception{ /** * */ private static final long serialVersionUID = 1L; public MyException(String Message) { super(Message); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException( Throwable cause) { super(cause); }}
public class ExceptionLinkInRealWorld {
public static void main( String args[] ) { try { throwExceptionMethod(); //有可能抛出异常的方法调用 } catch ( MyException e ) { System.err.println( e.getMessage() ); System.err.println(e.getCause().getMessage()); } catch ( Exception e ) { System.err.println( "Exception handled in main" ); } doesNotThrowException(); //不抛出异常的方法调用 }public static void throwExceptionMethod() throws MyException
{ try { System.out.println( "Method throwException" );throw new Exception("系统运行时引发的特定的异常"); // 产生了一个特定的异常
} catch( Exception e ) { System.err.println( "Exception handled in method throwException" ); //转换为一个自定义异常,再抛出 throw new MyException("在方法执行时出现异常",e);} finally { System.err.println( "Finally executed in throwException" ); }
// any code here would not be reached
}public static void doesNotThrowException()
{ try { System.out.println( "Method doesNotThrowException" ); } catch( Exception e ) { System.err.println( e.toString() ); } finally { System.err.println( "Finally executed in doesNotThrowException" ); }System.out.println(
"End of method doesNotThrowException" ); }}运行结果:
Method throwException
Exception handled in method throwExceptionMethod doesNotThrowExceptionEnd of method doesNotThrowExceptionFinally executed in throwException在方法执行时出现异常系统运行时引发的特定的异常Finally executed in doesNotThrowException