Monica Shiralkar wrote:Catching the type Exception is said to be a bad practice and catching the exact exception is good practice.
Monica Shiralkar wrote:Because both are giving me the same messages printed from catch block:
Paul Clapham wrote:?... and so you shouldn't catch them unless you know what to do with them. Just let them crash your program.
Paul Clapham wrote:Normally the container you're running your code in will show you the stack trace after the unchecked exception bubbles up to the top. You must have seen that -- don't tell me you never wrote a program which threw a NullPointerException.
Campbell Ritchie wrote:I suggest you shouldn't add such a message to a logging file. It will be easier automatically to handle the log file if it doesn't contain such messages.
Monica Shiralkar wrote:I thought I may add some text in my logging statement like "An error has occurred. The full stack trace is ->" unlike the case of default handler catching it.
We appear to have completely misunderstood each other.Monica Shiralkar wrote:. . . But if I dont catch it will not go in the log file at all. . . .
I think what Paul is referring to is what you do after logging the exception. Are you aware of the mechanics of bubbling up an Exception ?Monica Shiralkar wrote:... Thanks. Yes it shows, but I thought I may add some text in my logging statement like "An error has occurred. The full stack trace is ->" unlike the case of default handler catching it.
Monica Shiralkar wrote:Thanks all.Understood the disadvantage of using Exception type. If we use specific message we can put our specific message there for understanding it better when we log. I was thinking it will come from exception type itself but it has to come from our logging statement.
Paul Clapham wrote: If you're running a web application and one of your web components throws an uncaught exception, then the server will catch the exception and write it to the server log. In neither of those cases do you need to write special code to deal with uncaught exceptions, which is the reason why the Java designers invented unchecked exceptions. So that's two examples of the "default handler" which you claim doesn't write the stack trace anywhere.
salvin francis wrote:
Monica Shiralkar wrote:Are you aware of the mechanics of bubbling up an Exception ?
Yes. For the checked exceptions, from the inside layers, we dont catch the exception and instead use throws. Only in the outermost layer, we catch the exception.
Monica Shiralkar wrote:Yes. For the checked exceptions, from the inside layers, we dont catch the exception and instead use throws. Only in the outermost layer, we catch the exception.
Junilu Lacar wrote:that's basically saying "I will handle every manner of exception." That's fine if it's the last option in a chain of catch blocks where you filter out specific exceptions.
Junilu Lacar wrote:
If an unchecked exception that's caused by programmer error happens in production, then what you need to look into fixing is not only the bug but the problem in your development practice that allows such things to happen.
Junilu Lacar wrote:It depends entirely on the context where you handle the exception. Whether it's in an "inner" layer of your application or "outermost" layer has nothing to do with it.
I don't think that is correct. It is often better to keep nulls out of your code, which may involve throwing more NPEs in the development stage.Monica Shiralkar wrote:. . . we should not be catching NullPointerException and instead be putting checks in code.
No.Does this hold for each and every runtime exception?
Campbell Ritchie wrote: often better to keep nulls out of your code, which may involve throwing more NPEs in the development stage.
Monica Shiralkar wrote:put a null check condition in the code using ==. What does keeping nulls out code mean?
Agree there. Closing the app with System.exit(...); might do even more harm. I shall let you work out a few examples. Also why you shouldn't use 0 as the argument.Monica Shiralkar wrote:. . . catching the log (which is not required) and then doing system.exit(0) to make it exit. That would not make sense.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |