0

In java i have a method and a try catch that catches exceptions and also RuntimeException. Are there issues with catching the general RuntimeException?

jon
  • 253
  • 4
  • 8
  • 16
  • 3
    it is not an issue, as long as you know how to deal with it. – aran May 23 '13 at 16:22
  • 1
    Way too vague of a question. What exceptions are being caught before? How are the runtime exceptions handled? Show the code, in other words. – Paul Bellora May 23 '13 at 16:23
  • Depends on what you do with it. I prefer to catch specific exceptions, but since RTEs aren't declared, knowing all possibilities requires knowledge. The problem is that you may catch exceptions you *don't* specifically know how to handle. IMO it's better to let those bubble up. – Dave Newton May 23 '13 at 16:26
  • What i meant by this question is that are there runtime exceptions which, if caught, cause bigger problems. For example, catching out of memory exception is bad because you are out of memory. – jon May 24 '13 at 17:40

2 Answers2

1

You should catch errors/exceptions you can handle. Provided you do this you can catch anything. If you don't know what to do with the exception checked or otherwise I suggest you not catch it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

You catch RuntimeException for the same reason that you catch any exception: you plan to do something with it. Perhaps you can correct whatever caused the exception. Perhaps you simply want to rethrow with a different exception type.

Catching and ignoring any exception, however, is extremely bad practice.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289