26似乎没有
27.Modify Exercise 3 to convert the exception to a Runtime Exception.
package job;
import java.util.*;
public class Main {
public static void main(String[] args){
Main []a=new Main[1];
try{
a[1]=new Main();
}catch (ArrayIndexOutOfBoundsException e){
throw new RuntimeException(e);
}
}
}
output:
Exception in thread "main" java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at job.Main.main(Main.java:18)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at job.Main.main(Main.java:14)
28.Modify Exercise 4 so that the custom exception class inherits from RuntimeException, and show that the compiler allows you to leave out the try block.
package job;
import java.util.*;
public class Main extends RuntimeException {
String s;
Main(String s){
super(s);
this.s=s;
this.show();
}
void show(){
System.out.println("String="+s);
}
public static void main(String[] args){
throw new Main("wrong");
}
}
output:
String=wrong
Exception in thread "main" job.Main: wrong
at job.Main.main(Main.java:26)
29.Modify all the exception types in StormInning.java so that they extend RuntimeException, and show that no exception specifications or try blocks are necessary. Remove the '//!' comments and show how the methods can be compiled without specifications.
package job;
import java.util.*;
class BaseballException extends RuntimeException {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}
abstract class Inning {
public Inning() throws BaseballException {}
public void event() throws BaseballException {
// Doesn't actually have to throw anything
}
public abstract void atBat() throws Strike, Foul;
public void walk() {} // Throws no checked exceptions
}
class StormException extends RuntimeException {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}
interface Storm {
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}
public class Main extends Inning implements Storm {
// OK to add new exceptions for constructors, but you
// must deal with the base constructor exceptions:\
public Main()
throws RainedOut, BaseballException {
}
public Main(String s)
throws Foul, BaseballException {
}
// Regular methods must conform to base class:
public void walk() throws PopFoul {
} //Compile error
// Interface CANNOT add exceptions to existing
// methods from the base class:
public void event() throws RainedOut {
}
// If the method doesn't already exist in the
// base class, the exception is OK:
public void rainHard() throws RainedOut {
}
// You can choose to not throw any exceptions,
// even if the base version does:
// public void event() {}
// Overridden methods can throw inherited exceptions:
public void atBat() throws PopFoul {
}
public static void main(String[] args) {
try {
Main si = new Main();
si.atBat();
} catch (PopFoul e) {
System.out.println("Pop foul");
} catch (RainedOut e) {
System.out.println("Rained out");
} catch (BaseballException e) {
System.out.println("Generic baseball exception");
}
// Strike not thrown in derived version.
try {
// What happens if you upcast?
Inning i = new Main();
i.atBat();
// You must catch the exceptions from the
// base-class version of the method:
} catch (Strike e) {
System.out.println("Strike");
} catch (Foul e) {
System.out.println("Foul");
} catch (RainedOut e) {
System.out.println("Rained out");
} catch (BaseballException e) {
System.out.println("Generic baseball exception");
}
}
}
30.Modify Human.java so that the exceptions inherit from RuntimeException. Modify main() so that the technique in TurnOffChecking.java is used to handle the different types of exceptions.
package job;
import java.util.*;
class SomeOtherException extends Exception{}
class Annoyance extends RuntimeException {}
class Sneeze extends Annoyance {}
class WrapCheckedException {
void throwRuntimeException(int type) {
try {
switch(type) {
case 0: throw new Sneeze();
case 1: throw new Annoyance();
case 2: throw new RuntimeException("Where am I?");
default: return;
}
} catch(Exception e) { // Adapt to unchecked:
throw new RuntimeException(e);
}
}
}
public class Main {
public static void main(String[] args) {
// Catch the exact type:
WrapCheckedException wc = new WrapCheckedException();
for (int i = 0; i < 4; i++) {
try {
if (i < 3) wc.throwRuntimeException(i);
else throw new SomeOtherException();
} catch (SomeOtherException s) {
s.printStackTrace();
} catch (RuntimeException r) {
try {
throw r.getCause();
} catch (Sneeze s) {
s.printStackTrace();
} catch (Annoyance a) {
a.printStackTrace();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
}
output:
job.Sneeze
at job.WrapCheckedException.throwRuntimeException(Main.java:18)
at job.Main.main(Main.java:50)
job.Annoyance
at job.WrapCheckedException.throwRuntimeException(Main.java:20)
at job.Main.main(Main.java:50)
java.lang.RuntimeException: Where am I?
at job.WrapCheckedException.throwRuntimeException(Main.java:22)
at job.Main.main(Main.java:50)
job.SomeOtherException
at job.Main.main(Main.java:52)
结束~
我的另一个博客:https://www.cnblogs.com/hsjj/
会不定时的更新算法题
有问题欢迎发送邮件至hpzhangjunjiell@163.com
我们下次见哦~