Skip to main content

What are exceptions


Exceptions:
Exceptions are a set of mechanisms which are to be defined to do something when some problems happen during runtime. Some of the famous exceptions which occur normally are divide by zero exception and array out of bounds exceptions.
Java programming language has a great support for exception handling issues.
Exceptions are handled by using the try, catch ,throws and the finally statements.
The Try Block:
Use this block to enclose the area in the code which is exception prone.
The catch Block:
This block should immediately follow the try block. It contains the error message to be displayed and sometimes can be used to throw other exceptions.
Throws statement:
This statement is used to throw an exception .The exception can be predefined or user defined.
Finally Block:
This contains the set of statements which are to be executed no matter what kind of exception occurs in the program and is usually placed below the catch block.
Syntax:
function_name throws exception_name{//statements
try { //statements}
catch {//statements// may contain throws syntax in it as well}
finally {//statements that must be executed at all costs}
}
Exceptions can be checked or unchecked.
Checked exceptions are supposed to be handled by the programmer.
Unchecked exceptions are mainly due to bugs in the program or due to run time errors. Exceptions from which a program cannot escape at all are called errors. Errors make the program to just stop abruptly. Catch block should not be provided for errors.
Common Exceptions:
NullpointerException , securityException, NumberFormatException , FileNotFoundException etc.

User Defined exceptions can be created by Extending the exception class.
Public class userexpname extends Exception
{//statements}
To throw user defined exceptions You will have to use the throw clause.

Popular posts from this blog

How to make print screen button to automatically open the Snipping tool in Windows 11

 In windows you can search for keyboard from start menu and open keyboard settings.  Or you can go to Settings > Accessibility > Keyboard. In the keyboard settings window, toggle the switch that says "Use the Print screen button to open screen snipping".  Hereafter whenever you press print screen button, you will be able to make a selection on the screen for a screenshot.  

How to get Ribbon Style Interface in LibreOffice Write and Calc.

The ribbon interface in Microsoft office makes it easy for users to complete tasks easily. You can get a similar interface in LibreOffice by following the steps below: 1. Download the latest version of LibreOffice (at the time of writing this post, the version is 5.4) 2. Open LibreOffice Writer and Go to Tools > Options. 3. Under LibreOffice section, click Advanced. 4. Check the checkbox saying, "Enable Experimental Features". 5. Now go to View > Toolbars > select Notebookbar. 6. After restating, you should get ribbons. 7. You can still access the old layout by clicking on the three slider menu button and selecting MenuBar, to show the old style menu bar as well. If you want to change the tool bar style, you can select View (in the old style Menubar) > Toolbar Layout > Default. You can do the same for LibreOffice Calc as well.

How to measure exact power consumption ( in watts) of a device in your house

This works only if you have a digital energy meter. Switch off all appliances in your house, except the device whose energy consumption is to be measured. Now go to the energy meter and usually there is a single white button which toggles between a variety of display functions like Voltage, amps, watts etc. Press the button continuously until you see the Watts information. Some energy meters will only display energy consumed in kW. So if you see .04 kW, that means the device is taking 40 watts, Note that kWh is different from kW. Say if you have a fan that consumes 40W. If you run the fan for one hour, your energy in kWh will be 40/1000 (conversion to kW - divide by thousand) *1 hour = .04 kWh. If you run the fan for 10 hours then 40/1000 * 10 = .4 kWh Usually, 1 kWh = 1 unit of electrical energy. So if you have a device which runs in lower Wattage, you can run it for longer hours for the same amount of kWh consumed. Thus saving on bills.