#try catch finally的使用
class TestSleep{
public static void main(String[] args)
{
try{
Thread.sleep(1000);
}catch(Exception e)
{
e.printStackTrace();
}finally
{
System.out.println("finally.......");
}
}
}
#常throw RuntimeException的使用,运行结构
class Exception01
{
int age;
void set_age(int age)
{
if(age<0)
{
RuntimeException e = new RuntimeException(" age must > 0");
throw e;
}
this.age=age;
}
}
//测试代码
class Test{
public static void main(String[] args)
{
Exception01 person = new Exception01();
try
{
person.set_age(-10);
}catch (Exception e)
{
e.printStackTrace();
}
}
}
#抛出异常 throws
class Exception01
{
int age;
void set_age(int age) throws Exception
{
if(age<0)
{
Exception e = new Exception(" age must > 0");
throw e;
}
this.age=age;
}
}
//测试代码
class Test{
public static void main(String[] args)
{
Exception01 person = new Exception01();
try
{
person.set_age(-10);
}catch (Exception e)
{
e.printStackTrace();
}
}