class a extends Exception
{
a()
{
}
a(String s)
{
super(s);
}
}class a11
{
static void f(int b) throws a
{
if (b>10)
{
throw(new a("出错啦"));
}
System.out.println("a1*a1:="+(b*b));
}
}
class Q
{
public static void main(String[] args)
{
try
{
//a1 a1= new a1();
a11.f(50);
a11.f(5);
}
catch (a e)
{
System.out.println(e.toString());
}
}
}
当我第一次输入的数据错误(f(50))的时候 后面的数据(f(5))也被跳过了 这种情况怎么处理呢?谢谢
{
a()
{
}
a(String s)
{
super(s);
}
}class a11
{
static void f(int b) throws a
{
if (b>10)
{
throw(new a("出错啦"));
}
System.out.println("a1*a1:="+(b*b));
}
}
class Q
{
public static void main(String[] args)
{
try
{
//a1 a1= new a1();
a11.f(50);
a11.f(5);
}
catch (a e)
{
System.out.println(e.toString());
}
}
}
当我第一次输入的数据错误(f(50))的时候 后面的数据(f(5))也被跳过了 这种情况怎么处理呢?谢谢
class a extends Exception
{
a()
{ }
a(String s)
{
super(s);
}
} class a11
{
static void f(int b) throws a
{
if (b>10)
{
throw(new a("出错啦"));
}
System.out.println("a1*a1:="+(b*b));
}
}
class Q
{
public static void main(String[] args)
{
try
{
//a1 a1= new a1();
a11.f(50);
}
catch (a e)
{
System.out.println(e.toString());
} finally { ///放到finally里就可以了
try
{
a11.f(5);
}
catch (Exception e)
{
}
}
}
}
{
//a1 a1= new a1();
a11.f(50); //异常,就被catch了,下面的当然不执行了,你可以分别catch
a11.f(5);
}try
{
//a1 a1= new a1();
a11.f(50);
}
catch (a e)
{
System.out.println(e.toString());
}
try
{
//a1 a1= new a1();
a11.f(5);
}
catch (a e)
{
System.out.println(e.toString());
}
{
//a1 a1= new a1();
a11.f(50);
}
catch (a e)
{
System.out.println(e.toString());
}
try
{
//a1 a1= new a1();
a11.f(5);
}
catch (a e)
{
System.out.println(e.toString());
}
比如输入的是一组数据 发现其中有不符合的就跑出错误
总不能一个一个地try吧...
这样才能达到你的预期目的!
一般多个都是数组或者能由变量控制的,所以只要放循环语句就行了。
比如你这题,你要多个,你就放数组啊。
由键盘不断输入也行啊。
{
a() {}
a(String s)
{
super(s);
}
}
class a11
{
static void f(int b) throws a
{
if (b>10)
{
throw(new a("出错啦"));
}
System.out.println("a1*a1:="+(b*b));
}
}
class Q
{
public static void main(String[] args)
{
int[] i ={50,5,1,2,11,4};
for(int j=0;j<i.length;j++)
{
try
{
a11.f(i[j]);
}
catch (a e)
{
System.out.println(e.toString());
}
}
}
}