UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Java exception handling in real time examples eavry application how we are interacting with exceptions

Soma Sathya Prakash
2 days ago 0 0

Exception Handling:
====================
For example, gmail account, we need emaid and passwrd
if we entered correct account will be opened succusfuly
if enterd wrong email or passwrd it shows error message "please enter valid email or passwrd"
* that messege is genereted with execption,and it is user understandable language.
created by the proggramer
if we are not using exceptions system will be generated some message like"ORAQW12QSH".
user unable to understand system generated messages.

ex:

public class ExceptionEx{
public static void main(String args[]){

Scanner sc= new Scanner(System.in);

System.out.println("Enter first number");
int fnum=sc.nextInt();
System.out.println("Enter second number");
int senum=sc.nextInt();

int div=fnum/senum;
System.out.println(div);
System.out.println("remaining 2000 lines of code");
}
}



o/p:::

exeception occure in 30 line but we didnot mension any exceptions, it will throew error
there only stop the execution , remianing lines of code also not executed, is called abnormal
termination


Without exception Handling:
2.system defined messages()
1.abnormal termination (wherever problem occur adn the same line it will stop the execution)



Exception handling is a mechanisam of converting system error messages into user friendly messages.
2. user friendly
1. normal termination
*try, catch,finally, throw,throws

Before going to discuss these keywords we can discuss errors and exceptions.

Errors are two types.
They are
1)compile time error
2)Run time error(exceptions)

eg:System.out.println() ===>(compile time error)

** Compile time errors are occuring because of poor understanding of the language.

**Run time errors are those which are occuring in program when the user inputs invalid data

=>the run time errors must be always converted by the java programmer into user friendly messages using
the concept of Exception Handling.

Types of Exception:

we have 3 types of exceptions

1)checked exceptions
2)unchecked exceptions
3)Errors

Object java API

Throwable

1)Pre defined 2)User defined

1)Asynchronous 2)synchronous

java.lang.Error deals with program run time
deals with the hardware java.lang.Exception java.lang.RuntimeException
and external problems 1) checked 2)Unchecked
(hard disk) (main memory)
*BootStrapMethodError
*InternalAcessError *FilenotFoundException *ArithematicException
*SecurityExceptionError *ClassNotFoundException *NullPointerException
*OutOfMemoryError *IlligalAcessesException *ClassCasteException
*StackoverflowError *UnsupportedOperationException *IndexOutOfBoundException
*UnknownError *InterruptedException *ArrayIndexOutOfBoundException
*VirtualMachienError *StringIndexOutOfBoundException
*IlligalArgumentException
*NumberFormatException




try
{
//error code
}
catch(exception e)
{
//error handlig code
}
finally
{
//closing the connections or files
}


throw eg:
--------

class ThrowEx{

public int pass(int marks){

if(marks<35)
throw new ArithmaticException("you are failed");
}else
{
Syso("you are passed :" +marks);
}
public static void maon(String args[]){

ThrowEx m= new ThrowEx();
m.pass(33);

}

}

 

how to create user defined exceptions:
--------------------------------------
take your class and your class must be extends to any other parent exception

1.IdNotFoundException extends RuntimeException/Exception

2. your class need to take param constructor


eg:

class MyException extends Exception{

private int age;

public MyException(int age){
this.age= age;
}

public String totring(){
return "you are not eligeble for vote:"+age;
}
}
public class Exception6{

static void validation(int age) throws MyException{
if(age<18)
throw new MyException(age);
else
Syso("you are eligeble for vote");
}

public static void main(String args[]) throws MyException{

Exception6.validation(16);
Syso("rest of the code");

}
}

 

 

 

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Full stack java training demo for both frontend and backend with database
core java:========== lanaguage:---------- to communicate with person General languages: ------------------ engilsh,hindi,telugu,tamil programming language: =================== to machine--->binary...

Looking for Java Training Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more