Exceptions in java
Intro
Exceptions are errors in programming languages
Types of errors:
1. Syntax: if you write the keyword name incorrect it will throw error this is syntax errors.
2. Runtime: these are crucial they occur during the execution of code and user accessing.
3. Logical: these can be verified during compile time.
This is a critical statement it will give us error and we need to handle these errors
So what programming standard states that you have to write normal statements normally
and critical statements should be enclosed in try block , if try block throws error , you have to
catch the error
For particular statements you can apply try catch and for whole class we need to extend it by
exception class
exception classes hierarchy
Exceptions we can handle , but error s like jvm not working properly we cannot handle it
we can have one try and more catch statements
exception is the superclass of all the exceptions and exception class is the subclass of
throwable
c sends exception to b and so on to a
Throws means you are sending the exception to other class
lets first print e
here we have added some lines below the critical statement and
but due to exception these lines are getting skipped and directly execution is going to catch
statements
last catch block as exception
We should keep exception only at last because if we mention them in first place , it will
handles all the exceptions and what is the use of other two exception classes
finally block is printed in all the scenarios
If we know our program doesn’t throw any exception, then will not use try, catch and we will
throw exception class
you can mention many but this will not handle error but only suppress it
Throws should be only after method if
If my output is < 10 it will throw error
Sometimes you want to check the value which is entered should be greater than 10
user defined exception
but I want to print error , we should call constructor of exception class