public void getCustomerInfo() {
try {
// do something that may cause an Exception
} catch (java.io.FileNotFoundException ex){
System.out.print("FileNotFoundException!");
} catch (java.io.IOException ex){
System.out.print("IOException!");
} catch (java.lang.Exception ex){
System.out.print("Exception!");
}
}
举一反三
- getCustomerInfo()方法如下,try中可以捕获三种类型的异常,如果在该方法运行中产生了一个IOException,将会输出什么结果() A: IOException! B: IOException!Exception! C: FileNotFoundException!IOException! D: FileNotFoundException!IOException!Exception!
- Java中,下面捕获异常的语句不正确的是( )。 A: try{……}finally{……} B: try{……}catch(Exception ex){……} C: try{ try{……}}catch(Exception ex){……} D: try{ try{……} finally{……}}catch(Exception ex){……}
- 有某方法如下,try中可以捕获三种类型的异常,如果在该方法运行中产生了一个IOException,将会输出什么结果?() A: IOException B: Exception C: ExceptionIOException D: FileNotFoundExceptionExceptionIOException
- 以下哪一种是按照异常应该被捕获的顺序排列的() A: Exception、IOException、FileNotFoundException B: FileNotFoundException、Exception、IOException、 C: IOException、FileNotFoundException、Exception D: FileNotFoundException、IOException、Exception
- 读程序,写出和程序输出格式一致的输出结果。 public class J_Test { public static void mb_method(int i) { try { if(i == 1) throw new Exception(); System.out.print("1"); } catch(Exception ex) { System.out.print("2"); return; } finally { System.out.print("3"); } System.out.print("4"); } public static void main(String[] args) { mb_method(0); mb_method(1); } }
内容
- 0
请说出下列程序的输出结果_____________,_____________。import java.io.IOException;public class E {public static void main(String args[]){try { methodA();}catch(IOException e){System.out.print("你好");return;}finally {System.out.println("thanks");}}public static void methodA() throws IOException{throw new IOException();}}
- 1
import java.io.IOException; public class ExceptionTest( public static void main (String[]args) try ( methodA(); ) catch (IOException e) ( system.out.printIn(“Caught IOException”); ) catch (Exception e) ( system.out.printIn(“Caught Exception”); ) ) public void methodA () { throw new IOException (); } What is the result?() A: The code will not compile. B: The output is caught exception. C: The output is caught IOException. D: The program executes normally without printing a message.
- 2
(9-3)请阅读程序,然后写出程序运行结果。(请注意输出不换行) public class Demo3 { public static void main(String[] args) { int a=9,b=0; double x=9,y=0; try { int c=a/b; }catch(Exception e1) { System.out.print("1"); try { double z=x/y; }catch(Exception e2) { System.out.print("2"); }finally { System.out.print("3"); } }finally { System.out.println("4"); } } }
- 3
下面程序的执行结果是( )。public class Test {public static void main(String[] args) {new Test().test();}public void test(){try{System.out.print("try");}catch(ArrayIndexOutOfBoundsException e){System.out.print("catch1");}catch(Exception e){System.out.print("catch2");}finally{System.out.println("finally");}}} A: try finally B: try catch1 finally C: try catch2 finally D: finally
- 4
当编译并且运行下面程序时会出现什么结果? public class ThrowsDemo{ static void throwMethod() { System.out.print("Inside throwMethod"); throw new IOException("demo"); } public static void main(String [] args){ try{ throwMethod(); }catch(IOException e){ System.out.println("Cauht"+e); } } }