• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple Catch Block | Slows Down the Compilation Time| complexity measure.  RSS feed

 
Divyansh Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as Java is concerned, We should aim to catch and handle specific exception rather than General one. Won't it slow down the compiling time? As compiler has to check each and visit each and every catch block till it catches the exception. Why Can't we have a generalized catch block?

Thanks in Advance

Code
___________________________
void myfunction() {
try {
instruction1();
}
catch (ExceptionType1 e) {
// some code
}
try {
instruction2();
}
catch (ExceptionType2 e) {
// some code
}
try {
instruction3();
}
catch (ExceptionType3 e) {
// some code
}
try {
instruction4();
}
catch (ExceptionType4 e) {
// some code
}
____________________________________________
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It most likely will not slow down compilation time by any noticeable amount of time.

Write code so that it is clear, understandable and maintainable. That's much more important than writing code that compiles a fraction of a second faster.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the compiler does not visit each type of Exception. that is the runtime. I suggest you use javap -c Foo where Foo is your class name. You will then see that catch blocks are implemented as a sort of goto, which can be executed very fast.

The code example you posted does not permit a generalised catch. You are trying 4 different things and catching them individually. If you combine those 4 try‑s into a single try you will not create an equivalent program. You can demonstrate that by throwing an Exception from try 1. What you have written will then proceed to instruction 2; if you put the whole lot together in one try, instruction 2 will never be attempted.

Jesper de Jong wrote: . . . Write code so that it is clear, understandable and maintainable. . . .
The same applies to code posted here; please go back to your post and use the button to add code tags, otherwise the post is unformatted and difficult to read.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also...who cares if it slows down compilation time? For a small project, any difference would be unnoticeable. For a large project, you should be using a build tool so you only compile the dependent modules/parts.

Maybe back in the days when compiling took literally days it may have been an issue, but I can't see any reason why today you would care.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!