Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exception in Java

I'm currently in the process of learning how to properly do custom exception and I stumbled upon a problem. Whenever I try to utilize an object of a class that throws this custom exception, my IDE's debugger (I'm using IntelliJ idea) says "Unhandled Exception: InsertExceptionName()". The code, in a simplified manner, looks something like this. In this case, it should return an exception if the randomly generated number is <0.5, and return a number otherwise, but it won't do that. What am I missing?

public class main {
    public static void main(String[] args) {
        double x=Math.random();
        operation op=new operation();
        op.execute(x);
   }
}

-

public class operation {
    public operation() {
    }

    public double execute(double x) throws RandomWeirdException {
        if(x<0.5) {
            throw new RandomWeirdException("<0.5");
        }
        return x;
    }
}

-

public class RandomWeirdException extends Exception{
    public RandomWeirdException() {
        super();
    }
    public RandomWeirdException(String message) {
        super(message);
    }
}
like image 797
skulpt Avatar asked Jan 11 '15 18:01

skulpt


People also ask

How do you solve an unhandled exception in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What is unhandled exception type in Java?

Java provides complete support to handle the exceptions so that code can run smoothly without termination and provide the desired result. An exception that is not handled is called an unhandled exception and leads to terminating the code before its execution.

What happens when an unhandled exception occurs in Java?

An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

What are unhandled exceptions?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions. Learn about the definition and examples of unhandled exceptions, and explore programming and exception handlers.


1 Answers

What do you mean "return" an exception? When an exception is thrown, it bubbles up the call stack.

You are not handling it in this case. It reaches main and thus you have an unhandled exception.

If you want to handle an exception, you'd use a try-catch block. Preferably surrounding main in this case.

try {
    // Code that might throw
    // an exception.
} catch (Exception e) {
    // Handle it.
}

Another solution would be to specify that main throws a "RandomWeirdException", and not catch it in the first place.

public static void main(String[] args) throws RandomWeirdException { ... }

It's preferable to just let functions throw, unless you can reasonably handle the exceptional case. If you just catch for the sake of catching without doing anything meaningful in an exceptional case, it's the equivalent of hiding an error sometimes.

like image 57
user123 Avatar answered Sep 28 '22 11:09

user123