Reputation: 41
I have come across exceptions many times especially checked exceptions.When the syntax of the code is correct why should we have to put in try-catch block. if we don't put into try-catch block it will give error. please explain me about checked exceptions.why would some code will throw exceptions even the syntax is correct.
Upvotes: 0
Views: 97
Reputation: 1219
Exceptions has nothing to do with syntax errors. An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons.
For example, you can see the exceptions in following reasons:
These exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
To make the program work smoother, the program should handle the exceptions properly with logic.
Upvotes: 0
Reputation: 2924
Syntax isn't the only resource for Exceptions. Invalid indexing, Mathematical errors, Connection issues, Db structure incompatibilities do cause Exception too! Unless you are not handling a spesific type of Exception, the purpose of catching general Exceptions is to manage them. If you are not intented to manage them, you may not catch them at all. But whatever the situation is DO NOT SUPPRESS Exceptions. If you have to handle some erronous situation use logical locks.
Exceptions are for exceptional situations only, and should not be used for data validty. Exceptions are way too much costly for that.
Upvotes: 0
Reputation: 10840
Expections have nothing to do with illegal syntax. Excpetions are used in cases, where an error happens that can't be known about when the code is written or compiled, one example would be that there is no more memory available.
For languages which are compiled (e.g. Java) Expections are thrown while the program runs. On the contrary, syntax errors are handled by the compiler, at compile-time.
Upvotes: 1