r/learnjava • u/JAVAJAVAJAVAJAVAJAVA • Feb 09 '17
Exception Handling question regarding "throws" [Will gild]
Let me get this straight.
If I have code that may throw an exception, I enclose this code in a throw block right?
Then, to deal with the exception I have to proceed with catch blocks.
Suppose I identified the type of exception that may occur and I create an exception block for it. I've successfully handled the exception then.
Why is the THROWS needed?
When creating a method, it is said that you need to specify the type of exception that will occur if the exception is checked. How does Java determine if an exception is going to occur during compile time, and if such an exception checked and not unchecked, or does it not determine that and simply halt the program when an checked exception that is not accounted for occurs?
I'm guessing if I don't specify that, say the method Circle(double radius) may throw a IllegalArgumentException and a negative raidus was entered the program would terminate right and not be able to proceed or be dealt with? In this case I specify the specific exception because that exception is checked?
If I specify that the exception is checked, why is it said that Java needs to propogate throw a check stack to see which exception occurred matches the exception in the callstack when I already told it in this instance?
I will GILD whoever helps me out and explains everything in details. I really mean it, thanks.
2
u/[deleted] Feb 09 '17 edited Feb 09 '17
I'm not an expert, but I'll give it my best shot. There are a few main things you need to know about exceptions: exception propagation, unchecked and checked exceptions, and exception handling.
Exception propagation: Programs have a main method, and this method calls methods, which can call methods, etc. I like to think about the propagation of an exception like the hierarchy of a business. Suppose an employee comes across a problem that isn't in his job description and he doesn't know how to handle it. He goes to his manager. Suppose the manager doesn't know either. The manager goes to the vice president. This happens until someone higher up knows how to handle it, or until it reaches the top (at which point the exception is handled or propagated out of the main method and the program terminates).
When an exception is encountered and not handled, the program will terminate and print out what is called the stack trace. This is basically the list of methods that the exception propagated out of starting with where it originated and ending with the main method.
Unchecked exceptions: All exceptions are children of the Throwable class. An unchecked exception is a special type of exception that are only detectable during run time. This means the exception handling of unchecked exceptions is not managed during compiling. An unchecked exception happens if you have ever divided by 0 or tried to reference a value in an array that was outside of its range. They only occur due to bad programming. All unchecked exceptions are children of the RuntimeException or Error class.
Checked exceptions: Checked exceptions are all exceptions that are not unchecked exceptions. These exceptions are verified during compile time and you need to make sure you do one of two things. If you generate a checked exception somewhere, then you need to either handle it or tells the compiler that the method can throw it to the method that called it. If you do throw it to the method that called it, then the compiler requires the method that called it to either handle it or pass it on to the method that called it, all the way up to the main method. This is similar to the business hierarchy analogy that I talked about before.
Exception handling: Exception handling is exactly what it sounds like. If an exception occurs in a method, then that method may throw the exception to the method that called it. The method that called it can have the ability to catch the exception that was thrown to it. In order to do this, it needs to have a try-catch block. If the call to the method was done in a try block and the called method throws an exception, then the calling method will check to see if the thrown method has a catch block that is specific to the type of exception that was called. If it does, then the instructions associated with that catch block will be executed. Also, all of the code in the try block that happens after the exception throwing method will not execute.
If the method in the try block has the ability to throw a checked exception that is not handled in a catch block, then the method with the try block must also contain a throws clause in its signature for that exception. The compiler has the ability to check whether or not the rules for these checked exceptions are being followed. If they are not, then the program will not compile.
Also, unchecked exceptions can also be thrown and caught, but they usually aren't since the occurrence of one means that there is a problem with your program that should probably be addressed instead.