DEV Community

Cover image for Exception Checked VS Unchecked
Wagner Negrão 👨‍🔧
Wagner Negrão 👨‍🔧

Posted on

Exception Checked VS Unchecked

Hello, today I’ll speak about exceptions often when programming we came across with exceptions and use the advantages of IDE that we use to write the exception more adequately, but know only this isn’t enough, we must understand that exist two types of exception the checked and unchecked, to throw the exception adequate.

Firstly, the exceptions have a hierarchy all inherit of Exception. Each exception that extends creates your type of exceptions, but each exception has types like checked and unchecked that identify like will work into code, see the image below.

Structure of Exceptions

The exceptions checked are present in operations that represent an application risk, they can be native or not, like example SQLException, since all these are inherited directly from java.lang.Exception, father of all exceptions in Java.

See the example below:

public double calc(double a, double b) {
        throws SumException {
            double value = (a)/(b*b);
            if (value <= 18.5) {
                throw new SumException("Example exception checked !");
Enter fullscreen mode Exit fullscreen mode

The exceptions checked are treated or declared in the header of a method, because are related to the structure of the code and present the risk of exceptions, in this case, are switched on with exceptions that extend Exception. Basically, exceptions checked are related to the structure of code and extends of Exception, which can be exception native or exception created.

The exceptions unchecked are present in the run time of compilation, have inherited of RunTimeException, these exceptions aren’t expected directly when writing an application like example NullPointerException, this exception possibly will occur in run time.

See the example below:

try {
        File file = new File("path");
        FileInputStream fis = new FileInputStream(file);
    } catch(FileNotFoundException e) { ... some code ...}
Enter fullscreen mode Exit fullscreen mode

The exceptions unchecked will occur in rum time, case not it is treaty although not need of treaty, must be taken into consideration. Basically, exceptions unchecked are throw in run time and extend to the RunTimeException class.

Into the book, The Java programming language says "exceptions of run time unchecked represent conditions that, in general, reflect an error in logic into your program and not can be reasonably recovered at run time". So we should pay attention when we write code and create the exception, see the structure of exceptions.

When throw exception and which?

The exception is something powerful to an error handler in a programming language, so respect the structure in layers. Should create the politic of exceptions into of project to check and throw an exception, a good practice creates exceptions to specific cases and not return exception pure but clarify better that is happening. Now, which exception use to create this depends on the code that you are writing . No silver bullet.

Discussion (0)