1

I am studying OOP, and I did not understood the concept of exception.

What are the correct uses of exceptions?

Why use exceptions when you already know a possible exception?

For example, I have seen a code sample where the programmer needed to access a file, and had an exception in case the file does not exist. Something like "catch(fileDoesNotExist e)".

Why not use an if to verify before take the action? And use exception only for not known issues, for logging or error messages.

dbugger
  • 15,868
  • 9
  • 31
  • 33
user2977500
  • 147
  • 10
  • 1
    Firstly, checking first and then performing an action leads to [race conditions](https://en.wikipedia.org/wiki/Race_condition). Secondly, exceptions allow you to handle the error condition further up the call stack, rather than at the point of the error. – Phylogenesis Jan 30 '17 at 11:21
  • If I understood right, I can have better results in performance using Exceptions, maybe the reason is that I would not lose resouces checking possible error, and if they happen I already have a solution? – user2977500 Jan 30 '17 at 12:31
  • I did not understood this part "exceptions allow you to handle the error condition further up the call stack, rather than at the point of the error." – user2977500 Jan 30 '17 at 12:32
  • See [this question](http://stackoverflow.com/questions/10633664/what-is-exception-propagation). – Phylogenesis Jan 30 '17 at 13:14

2 Answers2

1

The idea behind the concept of exception was to decouple the error handling code from the "normal" behaviour flow control. That lets to manage/handle the exception further up the call stack.

Historically, with structured language, error handling code (file opening error,...) was mixed within the "business" application code. It was also painful to improve the code in order to manage new error codes.

What are the correct uses of exceptions?

If it is not normal that your file doesn't exist or cannot be opened => it is considered as an exceptional situation => exception => exception handler

Why use exceptions when you already know a possible exception?

To decouple the business application code from the error handling. That eases source code readibility and maintenance.

Jeandey Boris
  • 743
  • 4
  • 9
0

Exception:

Exception is that interrupt(break) the normal flow of the program.It's thrown at runtime.

Exception Handling

Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc

In java there are mainly two types of exception that checked and unchecked.Other than Error is there

Hierarchy of Exception classes in Java

Why use exceptions when you already know a possible exception?

basically exception handling use to mainly,we assuming in that our particular code will occur some(NullPointerException,ArrayIndexOutOfBoundsException etc..)exception.If we not Handle that,program will break.Actually that Exception it may or may not will happen.But so we need to handle normal flow of the program it occurred or not.Otherwise after that particular code section not executing.

Musni
  • 73
  • 1
  • 16