62,614
社区成员
发帖
与我相关
我的任务
分享
异常表示程序运行过程中可能出现的非正常状态,运行时异常表示虚拟机的通常操作中可能遇到的异常,是一种常见运行错误。java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。
一般来说,不抛出运行时异常(RuntimeException)或不创建一个运行时异常(RuntimeException)的子类的原因是不想为指定包含有运行时异常的方法所能抛出的各种不能估计的异常而操心。
public class ExceptionTest
{
private void exceptionTest()
{
ETest etX = new ExTest1();
String strTestString = etX.getTestString();
}
public static void main(String[] args)
{
ExceptionTest et = new ExceptionTest();
et.exceptionTest();
}
}
public ETest{}
class ETest1 extends ETest
{
public String getTestString()
{
return null;
}
}
class ETest2 extends ETest
{
public String getTestString()
{
return "Hello runtimeException";
}
}
MyClass o = MyFacory.getInstance();
o.getId();
MyClass o = MyFacory.getInstance();
if(o!=null){
o.getId();
。。
}
。。
URL url = new URL("http://www.csdn.net");
HttpURLConnection con = (HttpURLConnection )url.openConnection();