Difference between throw and throws in Java
This needs understanding of exceptions.
So first let us understand what exception is. An exception is an event that disrupts the normal flow of the program.
And we have Two types of java exceptions:
- Checked exceptions
- Unchecked exceptions
What is a checked exception?
Exceptions which are checked at compile time are called as checked exceptions.
These exceptions are also called as compile time exceptions. Like file not found exception
SQL exception and so on. These all comes under checked exceptions.
unchecked exceptions?
Exceptions which are not checked at compile time and these exceptions occurs only at runtime.
These exceptions are called as unchecked exceptions, and these exceptions are also
called as runtime exceptions as these occur at runtime.
Example: Arithmetic exception,like dividing by zero, null pointer exception and so on
comes under unchecked exceptions. Now let us understand what throw is.
Throw is a keyword in Java, so we can't use it as an identifier and it is used to explicitly throw an exception from a method or a block of code. It's usually a runtime exception and just now we understood right runtime exceptions occur at runtime and developer should not actually try to catch it or stop it. So instead we need to write the code to avoid it. Or we can issue a command throw error.
The error criteria is met, right?
So syntax of throw. Is true instance and this here instance can be of type throwable class which is present in Java Lang package or any of its subclasses. Any of this subclass of throwable class. So this is the syntax of throw. Now let us understand what the throws is throws is also a keyword and it is used in the signature of the method to indicate that this method might throw one of the listed type exceptions.
So this is like a warning or this is like, you know, just an information for a calling method where is calling the method which has the throws will come to know that the exception that the method returns. So usually this will be a checked exception.
And syntax of throws is so it comes in the method signature, right?
So return type of the method, method name, parameters and actually throws keyword and the list of exceptions. And here the list of exceptions are separated by comma. Now, let us see an example on throw and throws.
Let us create a method.
- Division. Where it takes 2 parameters.
- a and b and we are going to divide a by b.
- We are interested to return a by b.
- And what if? Divisor is zero, it becomes infinite, right?
- So we have. And this is an exception.
So we can actually throw this exception when the divisor is 0. So A is a dividend here, and B is the divisor. So when b becomes zero that becomes an exception. So any number divided by zero becomes infinite, right? So now we wanted to throw this end exception.
So I'm using keyword throw here and then followed by the. Instance of throwable class right. So for that I'll use new Arithmetic exception. And here. In the exception, we can actually pass some. Message as a parameter. I'll just say divide by zero. And you can see here throw followed by instance, and this incident should be the the subclass. Either it should be a type of throwable class or any of its subclasses, right?
So let's see what arithmetic exception is like arithmetic exception extends Runtime exception and runtime exception extends exception and you can see exception is actually extending throwable class right. So we can use this arithmetic exception here as per the syntax of throw Right? Now, let us call this method from the main method. And pass.
Some arguments, so the second argument should be like 0. So that what happens, this condition becomes true and it has to throw this arithmetic exception. And we can see here we get the exception arithmetic exception and the message whichever we have passed it as a parameter. With this developer gets an idea when there is some issue with this exception which is runtime exception. This arithmetic exception is a runtime exception and will actually try to avoid this exception.
Instead of catching it right?
This is about throw. Now, Let us understand what throws keyword is We'll see an example on throws now.
And for this Let us take an example of. Reading a file.
Where we wanted to read a file Using file reader class. Will pass some intentionally Will pass some invalid path. Right?
And then we'll take bufferedreader will create bufferedreader object Will pass file reader to it.
And then we'll actually try to Read first 3 lines of the file which we are reading. That's the program And how to read each line
using Bufferedreader is br dot. We have a method called. Readline will use that. And finally, we'll close bufferedreader.
Bufferedreader object, we are going to close right now. If you see we are getting some errors here, compile time exceptions, compile time exceptions you can see here unhandled exception file not found exception.
This is a compile time exception. So definitely we need to take some action here Add exception to method signature. Let's take this suggestion from the IDE so it says throws file not found exception so. So this says that whoever is the calling method of this method right, the read file so that method will come to know that this method will actually. Return the file not found exception.
There may be a chance that on calling this method we that method would get the exception of file not found file not found right and we also have another one so you can see here unhandled exception IO exception.
So reading the line and closing the buffer reader right might cause IO exception. So we need to also add this exception as well. So when I actually take this as a suggestion so it gets added to this list. But if you observe so the first exception file not found exception was removed and only ioexception got added here.
So forcefully we can actually add the file not found exception as well, but We can actually remove this as IO exception is a superclass of file not found exception and this will take care of file not found exception as well right?
So I'll just keep this as is. And now let's call this method from the main method. Or what we'll do is we'll create a calling method and see how to handle this. So we said it's up to the calling method how to handle this exception if this method is called right?
So let's call this method first read file. And you can see the error again. So remember, this was the, uh, checked exception and we actually. The use throws to throw this exception and we we are have a calling method. Now we are actually calling this read file method and if you observe again unhandled exception add exception to method signature.
So we have like 3 possibilities
Now how to handle this. So this is the calling method and it can handle this compile time error in three ways. Either it can add the exception to the method signature and throw the exception like how this method did or What we can do here is We can actually add the try catch block. We can surround this Statement with the method call with the try catch block you can see it here right?
This is also valid. Or what we can do here is we can even through this throw this exception e. in this case, again, we need to add the statement throws IO exception to the method.
These are the three possibilities how we can handle the the method call Which has the throws keyword, right? So we'll use this one and let and to call this method right? We need to create the main method and then We will call this method. And this has to be static to be called in. Main with it this is the calling method.
And we can see invalid path the system cannot find the find the file specified file not found exception. So as we gave, the invalid path here, so this returns the the file not found exception and the calling method actually handled that. So this is about the throws, throws keyword, and now we just not discussed checked exceptions, unchecked exceptions, right?
We also have another exception called custom exception. We can actually create a custom exception. So let's create that custom exception. I'll name my. Custom exception as my exception and I have to extend. Either throwable. throwable class or
any of its subclasses, right? So exception is a subclass of throwable class, so I wanted to create my own custom exception. So I'm extending the exceptions class as we can only throw the exceptions which are of the type throwable or any of its subclasses, right? So that's the reason I am extending exception class in my customs exception and I'll just create one constructor.
Which can take a parameter. Right, and we'll call the parent parent constructor. This is my custom exception. Now will add one method.
Where will say?
Throw custom exception and Here we'll just use throw and throw this exception will create a new instance of custom exception here and optionally we can pass this. Parameter message. And finally, we'll call this method and you can see whenever as soon as
we actually throw this exception, we are getting again compile time error to add the exception to that method signature, see.
We need to add this to the method signature.
Now we can call this method. Throw my exception again. It has to be static to be called in main method and Without creating the object of the class, of course. Now, We will call this method. And again, as this method is throwing my exception, so this has to be surrounded by trycatch.
And we can see we are getting the exception here as we are throwing exception if you observe. So execution starts. Here we are calling this method in try throw my exception and this method is called where we are throwing the myexception and my exception is the custom exception which we created here right and this is throw.
This is actually passing this message to it and we will get that into the catch block and this we are getting this statement. This is how we can actually write our own custom. Exception now let us see the differences between throw and throws. The first difference throw is used to create exception manually.
We create the exception manually and throws is used to declare the exception in the method signature right and throw is used inside a method right. We create a method and inside it we actually use the throw keyword and throws is used in the method signature itself. And throw is you mainly for runtime exceptions.
We just know, so right? So all mainly for runtime exceptions we use the throw keyword. And throws keyword is mainly used for the
checked exception compile time exceptions. Throw can throw only single exception and throws We can declare multiple exceptions just. separated by comma and throw, keyword should always be followed by instance and that incident should be a
class of throwable or any of its subclass is What we discussed and throws keyword followed by a class. Throw cannot include any statement after the throw.
Whatever we write the statements below the throw keyword, right, those are all ignored. And no such restrictions for the throw keyword. So these are the differences between throw and throws. That's all for this article. Thanks for visit hpkingdom.com...