(1)找出下面程序的错误并更正(提示:共有5处错误)。
public class MyClass
{
public static void main(String args[])
{
myMethod();
}
public void myMethod() throw MyException
{
throws (new MyException());
}
}
class MyException
{
public String toString()
{
return ("用户自定义的异常");
}
}我修改了几处,以下的就不对拉.....
public class MyClass
{
public static void main(String args[])throws Exception
{
myMethod();
}
public static void myMethod() throws Exception
{
try{
throw (new MyException());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
class MyException
{
public String toString()
{
return ("用户自定义的异常");
}
}
public class MyClass
{
public static void main(String args[])
{
myMethod();
}
public void myMethod() throw MyException
{
throws (new MyException());
}
}
class MyException
{
public String toString()
{
return ("用户自定义的异常");
}
}我修改了几处,以下的就不对拉.....
public class MyClass
{
public static void main(String args[])throws Exception
{
myMethod();
}
public static void myMethod() throws Exception
{
try{
throw (new MyException());
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
class MyException
{
public String toString()
{
return ("用户自定义的异常");
}
}
{
public static void main(String args[])
{
new MyClass().myMethod();
}
public void myMethod() throw MyException
{
throw new MyException();
}
}
class MyException
{
public String toString()
{
return ("用户自定义的异常");
}
}
{
public static void main(String args[])
{
new MyClass().myMethod();
}
public void myMethod() throw MyException
{
throw new MyException();
}
}
class MyException extends Exception
{
public String toString()
{
return ("用户自定义的异常");
}
}
你的代码可以改成:public class MyClass {
public static void main(String args[]) {
myMethod();
} public static void myMethod() {
try {
throw (new MyException());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}class MyException extends Exception {
public String toString() {
return ("用户自定义的异常");
}
}
或者:
public class MyClass {
public static void main(String args[]) throws Exception {
myMethod();
} public static void myMethod() throws Exception {
throw (new MyException());
}
}class MyException extends Exception {
public String toString() {
return ("用户自定义的异常");
}
}
throws Exception
throw new Exception
//在自定义的异常中
class MyException extends Exception{
public MyException(String message){
super(message);
}
}
{
public static void main(String args[])
{
try
{
new MyClass().myMethod();
}
catch (Exception e)
{
System.out.println(e);
}
}
public void myMethod() throws MyException
{
throw (new MyException());
}
}
class MyException extends Exception
{
public String toString()
{
return "用户自定义的异常";
}
}