Throw and Throws Keywords

2 minutes
Share the link to this page
Copied
  Completed
Throw vs Throws in java. 1. Throws clause is used to declare an exception, which means it works similar to the try-catch block. Throw keyword is used in the method body to throw an exception, while throws is used in method signature to declare the exceptions that can occur in the statements present in the method.

Transcript

Hello in this Java video we are going to look at the fro and froze keyword, just a keyword, there's two keywords froze and froze. Okay, so the fro keyword so this one right here will give us an error, we haven't fully coded the this keyword allows you to basically trigger an exception and the froze keyword, it would be attached to a method which we'll demonstrate in a second, which say that this method could potentially trigger this particular exception. So I'm going to create another method and I'm going to call it on a very clunky Okay, so here throws and then the exception so I'm going to stick with a simple one. arithmetic exception I don't important Modules are there, and we can throw it. So if you do throw, then you can the exception itself and listen to it. Like so run it, run fine.

We haven't actually call this method before to do fun. And run it. I think fees fraud the exception, you would probably include this in like a toy cast lock, like so. You try this catch the error if you're the person like so, and you can handle it in here when you do something. System dot print ln. And I'm gonna say a riff we'll get end of the application to the system.

Printf and write an essay, the arithmetic exception triggered. Oh yeah. And if I were to just comment this line out, however, it doesn't go to the catch block, because the exception was never triggered. So the beauty of this is you can, you know, call a method, for example, or like a bunch of methods, and they could potentially throw an exception, but you can handle it using cash blocks. And if something goes wrong, you can you know, try and, you know, continue, go in the make, try and make sure the application is still running, and not just crash and burn because it's not a good user experience when stuff just crush it. So that's it for this tutorial.

If you have any questions, feel free to reach out, and I look forward to seeing you in the next awesome Java video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.