Is there a limit to the number of exceptions I have to throw from a method?

Will declaring this feature have a performance impact?

public init(){
    try{
         initApplication();
     }catch(A1Exception){

     }catch(A2Exception){

     ...
     }catch(A5Exception){

     }
}

private void initApplication() throws A1Exception, A2Exception, A3Exception, A4Exception, A5Exception {
   initApp1(); //throws A1, A2, A3
   initApp2(); //throws A4, A5
}

      

Are there any problems implementing initApplication () this way?

+2


source to share


10 replies


In recent years, there has been a feeling that checked exceptions are pretty harmful. Each of these exceptions, if checked, causes the calling methods to handle or declare them. This breaks encapsulation because now some of the lower level implementation details are leaking out to higher levels.



Joshus Bloch talks about this in Effective Java , which I highly recommend.

+4


source


I don't see a problem with what it takes to exclude your code. My first impression when you look at the example is that Exceptions can be used here to control the flow of your application. Be careful not to. Exceptions should only be thrown in exceptional cases.

One of the reasons the process flow is not handled through Exceptions is that collecting exceptions is an expensive process. While the structure of multiple catch blocks shouldn't be a performance hit, a (potential) underlying process that uses Exceptions to control flow won't work well.



With this in mind, is there a "smell"? Only if the above problem is true in the design of the code.

+1


source


There is no limit as to how many exceptions a method can throw. The more you throw an exception, the more you can be specific that any exceptions will be thrown.

I just would like to point out a few suggestions that I am following. 1) At least you have a generic exception in the latter case, if any other exception that might happen in your code is caught than thrown into the calling class. 2) You can have a category of exception classes like BusinessLogic Exception, InvalidDataException, SystemsException, so you can have fewer exceptions thrown from any method. (Unless your business requires an exact exception 3) Always have error codes than throwing actual text messages that will make your application language independent.

+1


source


Q1 . Will this have a performance impact?

  • Not unless they are "genuine" exceptions (ie not used for normal program flow)

Q2 . Do you see the smell in initApplication()

?

  • No, no, assuming it initApplication()

    can raise N exceptions, i.e. if N is present, exceptional circumstances that may prevent the initApplication()

    completion of the work.
0


source


1: Performance impact will be minimal ...

2: But this is not necessarily a good idea, as having an excessive boiler plate code makes it difficult to read. If every exception will be handled the same way, then you should either catch the base Exception class. Or, you can force initApplication () to throw a custom exception - something like ApplicationInitializationException that conveys a little more meaning about what went wrong. You can set the exact message for the exception.

There are several cases where you might get 5 different exceptions and might have to deal with all of them in different ways. In this case, catching all 5 will be the right thing to do. But it's worth considering whether this is really necessary before you implement it.

0


source


If the exception does not affect performance, if an exception is thrown, there is some overhead of finding a matching catch that will increase as more exceptions are added, but that doesn't matter, because the exceptions must be ... exceptional. This should not happen under normal circumstances.

In other words, if you don't use a throw exception to control the flow of your program, you should be fine, and if you do, it certainly smells no matter how many clan proposals you have

0


source


On a purely technical note, the file format class

introduces an upper limit on exceptions 65536

. Type u2

exceptions_table_length

is an abbreviation for unsigned two-digit bytes.

The Code attribute has the following format:

Code_attribute {
 u2 attribute_name_index;
 u4 attribute_length;
 u2 max_stack;
 u2 max_locals;
 u4 code_length;
 u1 code[code_length];
 u2 exception_table_length;
 {     u2 start_pc;
        u2 end_pc;
        u2  handler_pc;
        u2  catch_type;
 } exception_table[exception_table_length];
 u2 attributes_count;
 attribute_info attributes[attributes_count];
}

      

I only use this if you are generating code. JSP developers often have to worry about problems with generated methods larger than 64KB. This and other limitations are listed in Section 4.10 Java Virtual Machine Limitations .

0


source


My guess is that this answer is mostly related to Q2, but the idea of ​​it, which is not so technical, but perhaps worth considering anyway, is the one behind the following two questions:

  • Does it make sense for the user to catch them?
  • You want to take a chance when the user is fed up with a lot of catch blocks, but instead just insert one "catch (Exception ex) {//ex.printStackTrace(); // enable this if we see problems}" or something something similar and stupid.?

In this case, I think it may not be relevant, but I think it is worth considering when developing the API in general.

0


source


Since no one seemed to mention this:

I would say that if a method throws many different exceptions, it can lead to many things. Especially a lot of different things.

0


source


Don't throw any exceptions unless you take action if an exception is thrown. Convert the selected exceptions to runtime exceptions for logging.

More information on this topic can be found here

0


source







All Articles