Java Exception Notes

 In Java exceptions are object that contains details about errors

In Java if exception occurs then it looks for its calling code that if they have an exception handler if the code that has occurred the exception has  an exception handler then it handles the exception if not it goes to its caller to handle the exception if it has the handle and then it handled the situation or else it checks for its caller to handle the exception if nobody has the exception handling in java as the default exception handler and in the program

In Java three types of exception checked exception or compile time exception unchecked exception or runtime exception and error

checked exceptions occurs and should be handled by the programmer

runtime exception ocas because of programming errors and we should anticipate and display the user a message

The third type of exception is errors and if they occur we should let the program crash and fix them

Exception hierarchy




You can get the message of an exception by using the exception reference variable dot get message function
Var.getMessage()

You can have multiple catch clauses in Java but remember the hierarchy the child should be up then the parent

If you is var then remember "this" that you can only use var for the object that is initialised if you just wanted to create a variable and do not want to initialise it or you want to initialise with null then you must write the data type of the variable

When you could have an exception you have two ways to handle it one use try catch block and handle it or other is write throws and exception name and tell the user of the class to handle it

Question 
when we the throws keyword
Ans 
When we get an exception we have two options either use try catch to handle it ourselves or to put throws keyword with the exception name to tell the caller of the object that the caller should handle the exception ,
this is where we use the throws keyword when exception occurs either we can handle the exception or if you want to re throw  the exception then we will write throws and then the exception reference variable usually e or ex then we are gonna tell that caller too handle it by writing throws

This is what we call at its option to method signature

For creating a custom exception you just need to create a java class with the last name exception then you are to decide whether it will be checked or unchecked exception if checked then extend the exception class if unchecked then extend the runtime exception class

If you write e.get message () then you can get the message that you have stored into your supers argument when defining your custom exception















No comments:

Post a Comment