异常处理的try catch
package Throwable;
public class TryCatchTest {
public static void main(String[] args)
{
int num1=30,num2=0;
int result;
//确定捕获异常的范围
try{
//该语句会产生除数为0的异常
result=num1/num2;
//System.out.println(result);
}
// 处理异常
catch(Exception e)
{
//输出与异常有关的错误信息
System.out.println(e.getMessage());
}
finally
{
System.out.println("这是最后TryCatchTest一个语句!");
}
}
}
2
package Throwable;
//public class MyException extends Exception
//{
// public String getMessage()
// {
// return("字符串长度已经超出范围!");
// }
//}
public class MyExceptionTest
{
public static void main(String[] args)
{
String str="this is my ExceptionClass Test!";
try
{
if(str.length()>5)
throw new Exception();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}