考虑到你有这样的代码:
doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.
现在我知道了,在构造异常时,特别是解开堆栈时,实际上
我的高级同事告诉我要将每个方法包装在一个try-catch块中,这样他们就可以跟踪异常发生的位置,从而帮助调试问题更快。是否最好将每个方法包装在这样的试捕获中:
Public int foo()
{
try
{
//do something
}catch(Exeception ex)
{
//do something with ex
}
}
还是在我认为可能发生的情况下捕获异常更好?例如,使用数组做某事可能会导致IndexOutOfRangeException的发生。
//wrap this in try catch
int[]
我见过有人说,使用没有参数的catch是不好的形式,特别是当catch不做任何事情的时候:
StreamReader reader=new StreamReader("myfile.txt");
try
{
int i = 5 / 0;
}
catch // No args, so it will catch any exception
{}
reader.Close();
然而,这被认为是很好的形式:
StreamReader reader=new StreamReader("myfile.txt");
try
{
int i = 5 / 0;
如果没有catch块,哪个异常处理程序将处理异常?
public class Doubt {
public static void main(String arg[])
{
System.out.println("hi");
int i=10;
int j;
try
{
j=i/0;//Arithmatic exception type object is created but no catch block so who will catch d e
在我的应用程序中,我有100 classes,每个类都包含4 methods。我使用try catch进行异常处理。我在每个方法中都编写了try catch,然后在我的应用程序中就会有400 try catch statement。它会影响我的应用程序的性能吗?Is it possible to handle the whole exception using one try catch statement in the whole application