} public static void main(String[] args) { Test.g(); Test.f(); }} 运行结果如下: 程序没有再运行下去噢。 java.lang.NullPointerException at test.Test.g(Test.java:19) at test.Test.main(Test.java:42) Exception in thread "main"
对啊 ,呵呵,异常不捕获就不执行了一般,但捕获了就好了哪怕你catch了不做任何事
对于任何java方法,如果不能完成任务,都允许通过一条备用的退出通道退出其执行过程。但是并不是退出程序。 比如这个程序,即使没有输入数字,捕捉到了NumberFormatException还是会继续执行下面的语句。 import java.io.*; class test { public test() { try{ System.out.println("Please enter a number:"); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(b.readLine()); } catch(IOException ex) { ex.printStackTrace(); } catch(NumberFormatException e) { System.out.println("The Exception has been caught."); } System.out.println("After caught"); } public static void main(String[] args) { new test(); } }
但是对于此构造函数外的函数运行,它就无能为力了,它只是保证了此函数的运行而已。 比如说下面的程序中hehe函数就不会运行了。 import java.io.*; class Test { public Test() { try{ System.out.println("Please enter a number:"); BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); int a = Integer.parseInt(b.readLine()); } catch(IOException ex) { ex.printStackTrace(); } catch(NumberFormatException e) { System.out.println("The Exception has been caught."); } System.out.println("After caught"); }
public static void hehe() { System.out.println("hehe"); } public static void main(String[] args) { new Test(); Test.hehe();
catch的作用就是让你捕获异常然后采取相应的手段啊,比如在catch{}里面添加从新连接的语句;
catch就是catch了,如果不是严重的错误,程序还是会执行下去的,所以要在catch子句里面添加相应的处理代码。比如可以弹出对话框什么的。出现异常并不一定是程序结束了
//提交事务
}
catch(Exception e){
//回滚
}
finally{
//释放相关资源
}
我是楼主!
String s = null;
s.length(); //will have NullpointerException
System.out.println("not used");
}
public static void f(){
try{
String s=null;
s.length();
}
catch(Exception e){
System.out.println("in catch:"+e);
}
finally{
System.out.println("in finally");
}
System.out.println("after finnaly");
}
我想我明白了,其实程序还会运行。不过只能运行到下次调用那个产生异常的那个东西的时候,比如你说的s.length(),如果再用到它,除非继续捕获异常,要不就不会再运行下去了,是吗?
里面的代码啊,你可以试试上面kingfish提供的代码 就知道执行的过程了
如果没有捕获异常有没有throw程序不会运行了吧
但是在程序推出之前finally里面的代码肯定是会执行的
public class Test
{
public static void g(){
String s = null;
s.length(); //will have NullpointerException
System.out.println("not used");
}
public static void f(){
try{
String s=null;
s.length();
}
catch(Exception e){
System.out.println("in catch:"+e);
}
finally{
System.out.println("in finally");
}
System.out.println("after finnaly");
}
public static void main(String[] args)
{
Test.g();
Test.f();
}}
运行结果如下:
程序没有再运行下去噢。
java.lang.NullPointerException
at test.Test.g(Test.java:19)
at test.Test.main(Test.java:42)
Exception in thread "main"
比如这个程序,即使没有输入数字,捕捉到了NumberFormatException还是会继续执行下面的语句。
import java.io.*;
class test
{
public test()
{
try{
System.out.println("Please enter a number:");
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(b.readLine());
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(NumberFormatException e)
{
System.out.println("The Exception has been caught.");
}
System.out.println("After caught");
}
public static void main(String[] args)
{
new test();
}
}
比如说下面的程序中hehe函数就不会运行了。
import java.io.*;
class Test
{
public Test()
{
try{
System.out.println("Please enter a number:");
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(b.readLine());
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(NumberFormatException e)
{
System.out.println("The Exception has been caught.");
}
System.out.println("After caught");
}
public static void hehe()
{
System.out.println("hehe");
}
public static void main(String[] args)
{
new Test();
Test.hehe();
}
}
{
Connection conn=DriverManger.getConnection();
}
catch(Exception e)
{
System.out.println(e.toString());
}Statement stm=conn.createStatement();//语句1