Exception Handling

  • Errors at run time are Exceptions
  • There are various exceptions that occur in java
  • Can be user generated- user can create exceptions by extending it to Exception class
  • throw keyword is used to deliberately generate an exception
  • throws keyword is used to indicate that an exception will occur

To handle exceptions we use try catch

try

{

code

}

catch

{

code

}

 

along with this finally keyword is used. Whether or not the exception occurs the code within the brackets of finally runs

package p2;
import java.util.*;
class m
{
public static void main(String s[])
{
Scanner scan= new Scanner(System.in);
try
{
System.out.println(“Enter your marks:”);
int mks= scan.nextInt();
if (mks<50)
{
throw new myexp(mks);
}
else
{
System.out.println(“Passed!!”);
}
}
catch(myexp m)
{
System.out.println(m);
}
}
}

Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s