public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n){
if(n == 0){
//抛出一个NullPointException
}else{
//抛出一个ClassCastException
//并设定详细信息为“类型转换出错”
}
}
}
答:
public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n){
if(n == 0){
//抛出一个NullPointerException
throw new NullPointerException();
}else{
try{
throw new ClassCastException();
}catch(ClassCastException e){
System.out.println("类型转换出错");
}
//抛出一个ClassCastException
//并设定详细信息为“类型转换出错”
System.out.println();
}
}
}
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
public class TestException {
public static void main(String[] args) {
System.out.println("main 1");
int n;
//读入n
ma(n);
System.out.println("main 2");
}
public static void ma(int n){
try{
System.out.println("ma1");
mb(n);
System.out.println("ma2");
}catch(EOFException e){
System.out.println("Catch EOFException");
}catch(IOException e){
System.out.println("Catch IOException");
}catch(SQLException e){
System.out.println("Catch SQLException");
}catch(Exception e){
System.out.println("Catch Exception");
}finally{
System.out.println("In finally");
}
}
public static void mb(int n)throws Exception{
System.out.println("mb1");
if(n == 1)throw new EOFException();
if(n == 2)throw new FileNotFoundException();
if(n == 3)throw new SQLException();
if(n == 4)throw new NullPointerException();
System.out.println("mb2");
}
}
问:当读入n分别为1,2,3,4,5时,输出的结果分别是什么?
答:
当n为1时:
main 1
ma1
mb1
Catch EOFException
In finally
main 2
当n为2时:
main 1
ma1
mb1
Catch IOException
In finally
main 2
当n为3时:
main 1
ma1
mb1
Catch SQLException
In finally
main 2
当n为4时:
main 1
ma1
mb1
Catch Exception
In finally
main 2
class MyException{}
class TestException {
public static void main(String[] args) {
ma();
}
public static int ma(){
try{
m();
return 100;
}catch(Exception e){
System.out.println("Exception");
}catch(ArithmeticException e){
System.out.println("ArithmeticException");
}
}
public static void m(){
throw new MyException();
}
}
答:
class MyException{}
public class TestException {
public static void main(String[] args) {
ma();
}
public static int ma(){
try{
m();
}catch(ArithmeticException e){
System.out.println("ArithmeticException");
}catch(Exception e){
System.out.println("Exception");
}finally{
return 100;
}
}
public static void m(){
throw new ArithmeticException();
}
}
public class TestTryCatch {
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma(){
int n;
try{
n = 10/0;
}catch(Exception e){
}
return n;
}
}
选择正确答案:
A.编译不通过
B.编译通过,输出-1
C.编译通过,输出0
答:A
public class TestTryFinally {
public static void main(String[] args) {
try{
ma();
}catch(Exception ex1){
}
}
public static int ma() throws Exception{
int n = 10;
int b;
//读入一个整数b
try{
System.out.println("ma1");
int result = n/b;
System.out.println("ma2" + result);
}finally{
System.out.println("In Finally");
}
}
}
在ma中,读入整数b,如果读入的值为10,则输出:。如果读入的值为0,则输出:_。
答:
ma1
ma21
In Finally
ma1
In Finally
public class TestException {
public static void main(String[] args) {
try{
System.out.println("main 1");
ma();
System.out.println("main 2");
}catch(Exception e){
System.out.println("In Catch");
}
}
public static void ma(){
System.out.println("ma1");
throw new NullPointerException();
System.out.println("ma2");
}
}
选择正确答案:
A.编译错误
B.编译正确,输出main1 ma1 In Catch
C.编译正常,运行时出错
答:A
import java.io.*;
import java.sql.*;
class TestException {
public static void main(String[] args) {
try{
ma();
}
/*1*/
catch(Exception e){
}
}
public static void ma() throws IOException{}
}
下面哪些代码放在/1/处可以编译通过?
A.catch(NullPointerException npe){}
B.catch(IOException ioe){}
C.catch(SQLException aqle)[}
答:AB