练习1:
public class E01 {
public static void main(String[] args) {
try {
throw new Exception("An exception in main");
}catch (Exception e){
System.out.println("e = " + e.getMessage());
}finally {
System.out.println("Finally!");
}
}
}
练习2:
public class E02 {
public static void main(String[] args) {
String s = null;
try {
s.toString();
}catch (Exception e){
System.out.println("Caught exception : " + e);
}
}
}
练习3:
public class E03 {
public static void main(String[] args) {
char[] array = new char[10];
try {
array[10] = 'x';
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("e = " + e);
}
}
}
练习4:
class MyException1 extends Exception{
String msg;
public MyException1(String msg) {
this.msg = msg;
}
public void printMsg(){
System.out.println("msg = " + msg);
}
}
class MyException3 extends Exception{
public MyException3(String s) {super(s);
}
}
public class E04 {
public static void main(String[] args) {
try {
throw new MyException1("MyException message");
}catch (MyException1 e){
e.printMsg();
}
try {
throw new MyException3("MyException3 message");
}catch (MyException3 e){
System.out.println("e = " + e.getMessage());
}
}
}
练习5:
class ResumerException extends Exception{}
class Resumer{
static int count = 3;
static void f() throws ResumerException{
if(--count > 0 )
throw new ResumerException();
}
}
public class E05 {
public static void main(String[] args) {
while (true){
try {
Resumer.f();
}catch (ResumerException e){
System.out.println("e = " + e);
continue;
}
System.out.println("Go through...");
break;
}
System.out.println("Successful!");
}
}
import java.util.logging.*;
import java.io.*;
class LoggingException1 extends Exception{
private static Logger logger = Logger.getLogger("LoggingException1");
public LoggingException1(){
StringWriter trace = new StringWriter();
printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());
}
}
class LoggingException2 extends Exception{
private static Logger logger = Logger.getLogger("LoggingException2");
public LoggingException2(){
StringWriter trace = new StringWriter();
printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());
}
}
public class E06 {
public static void main(String[] args) {
try {
throw new LoggingException1();
}catch (LoggingException1 e){
System.out.println("e = " + e);
}
try {
throw new LoggingException2();
}catch (LoggingException2 e){
System.out.println("e = " + e);
}
}
}
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;
public class E03 {
private static Logger logger = Logger.getLogger("E07");
static void logException(Exception e){
StringWriter trace = new StringWriter();
e.printStackTrace(new PrintWriter(trace));
logger.severe(trace.toString());
}
public static void main(String[] args) {
char[] array = new char[10];
try {
array[10] = 'x';
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("e = " + e);
logException(e);
}
}
}
class Thrower{
public void f(){}
public void g() throws MyException1{
throw new MyException1("Inside f()");
}
}
public class E08 {
public static void main(String[] args) {
Thrower t = new Thrower();
try {
t.g();
}catch (MyException1 e){
e.printMsg();
}
}
}
class ExBase extends Exception{}
class Ex1 extends ExBase{}
class Ex2 extends ExBase{}
class Ex3 extends ExBase{}
class Thrower2{
void f() throws Ex1,Ex2,Ex3{
throw new Ex1();
}
}
public class E09 {
public static void main(String[] args) {
Thrower2 t = new Thrower2();
try {
t.f();
}catch (Ex1 e){
System.out.println("e = " + e);
}catch (ExBase e){
System.out.println("e = " + e);
}catch (Exception e){
System.out.println("e = " + e);
}
}
}