/ExceptionExample.java
public class ExceptionExample
{
public static void main(String[] args)
{
try{
//使用给定字符串"I throw an Exception!!!"作为详细信息来构造 Exception 对象。
throw new Exception("I throw an Exception!!!");
}catch(Exception e) //可以捕获所有异常类型
{
System.out.println("Caught the Exception!!!");
//输出该对象的详细信息。
System.out.println("e.getMessage(): " + e.getMessage());
}
}
}
//ExceptionInherit.java
class MyException extends Exception
{
public MyException()
{
}
public MyException(String message)
{
super(message);
}
}
public class ExceptionInherit
{
public static void f() throws MyException
{
System.out.println("Throwing MyException");
throw new MyException("the second constructor!");
}
public static void main(String[] args)
{
try
{
f();
评论0
最新资源