N
N
NoMoneyException2016-07-27 19:12:21
Java
NoMoneyException, 2016-07-27 19:12:21

What expressions to use?

Hi) What train of thought / algorithm should be when choosing Exception'a?
For example, I wrote a class:

import java.util.Scanner;

public class Scan {
    private  Scanner scanner = new Scanner(System.in);

    public int scanInt() {
        try{
            return scanner.nextInt();
        }
        catch (Exception exc){
            return 0;
        }
    }
}

Ignore the code please, this is an example. Why would I use Exception instead of Throwable right away? Can all errors be immediately intercepted through Throwable? What's the point of using lower in the exception hierarchy? And another question regarding this example) Should I use Error-handling inside the method, or should I use throws and catch it in main() ? What to be guided by?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2016-07-27
@eugene_leshchinskiy

If the class does not do anything critical in the program, then it is possible and necessary to handle all exceptions within this class. If the class performs some critical actions, then most likely it can be very dangerous to handle exceptions within the class and it is better to catch them at a higher level.
Not a critical example: the class draws a picture on the screen, if a couple of pixels are somehow null during processing and you catch this exception by replacing the erroneous pixel with white, then nothing criminal will happen. But if you catch an exception a level higher, then there will be a lot of unnecessary crap and code.
A critical example: a class works with a database that stores data about people, an error occurs, say, with access to the database, you caught exceptions within the class itself and instead of returning, for example, "Ivanova A.P.", you have an exception, and instead of throwing an exception up, you pretend to be a hose and return that there are none. The code is somewhere further, seeing that everything is fine and "Ivanova A.P." is not present in basis quietly creates there new (connection with a DB could be restored by this moment) instead of updating record. Those. in such a situation, it is simply wrong to handle exceptions inside the class in most cases. iron concrete it must be processed higher and in case of an error in accessing the database, fall off and try everything again.

R
Rou1997, 2016-07-27
@Rou1997

Should I use Error-handling inside the method, or use throws and catch it in main() ?

In this example, you are writing your Util-class, of course, you need to fix the Java developers' defect in it, because nextIntthe one that throws a checked (checked) exception is an obvious excess that greatly complicates the code, so do the processing inside your Util-method, in case of an exception, return null, you can develop and apply your own logging mechanism, which is more flexible and convenient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question