How to use access specifiers in exceptions in Java?

How to use access specifiers in exceptions in Java?
How to use access specifiers in exceptions in Java? 


How to use access specifiers in exceptions in Java? 

Exception: A java Exception to understand the exception firstly we should know about the error in the program.

java Errors Two Types

1. Compile Time Errors

2. Run-Time Errors

Compile Time Errors: A compile-time Errors can be used caught during library reference, syntax error, or incorrect class import.it is called Compile-time errors.
 

Important Some Others Programming Language Exception 

👉C++ String Function 

👉C String Function 

👉Python Exception  


Run-Time Errors: A run time error that occurs during the run time of the program is called run time error.run time errors are user mistakes.

They are also known as exceptions. Hence we can say that exception is a runtime error that occurs because of a user's mistake.

Reasons of Exception

Incorrect Input: Let us suppose that we are entering our name in place of age causing exception because age is of data type integer and name will be a string.

File does not exist
: Suppose that we are reading a text file icoderweb.txt and that file does not exist in the system, causing an exception.

Exception related to array: Suppose that the array size is 5 and we are inserting more than 5 elements, causing an exception.

Divide by zero exception
: When a number is divided by zero then the output will be undefined.


Exception Handling: A Exception Handling process of handling the exception is called exception handling.
There are four main keywords are used to solve the problem of exception.

try block: A try block is a place where actual code is written and the exception occurs. when the code will lead to an error, that error/exception will get caught inside the catch block.

catch block: A catch block is intended to catch the error and handle the exception condition. We can use multiple catch blocks to handle different types of exceptions and perform different actions when the exceptions occur.

throw statement: It is used to show the user-defined message about the exception.

finally, block: This block executes either exception occurs or does not occur.


Syntax of try-catch

try

{

//Statement;

}

catch( ExceptionName 1 ) 


// catch block 


catch( ExceptionName 2 ) 


// catch block 


catch( ExceptionName 3 ) 


// catch block 



Try Block: A try block is a place where actual code is written and exceptions.


How To Use try-catch in Java?

import java.util.Scanner; 

class icoderweb 


public static void main(String[] args) 


Scanner obj=new Scanner(System.in); 

int x,y,z; 

try 


System.out.println("Enter First Number:"); 

x=obj.nextInt();

System.out.println("Enter Second Number:"); 

y=obj.nextInt();

z=x/y; 

System.out.println("Division Value:"+z); 


catch (Exception e) 


System.out.println("Error Message:"+e); 



}

*****Output***** 

First Time Run

Enter First Number:36

Enter Second Number:9 

Division Value=4 

Second Time Run

Enter First Number:78 

Enter Second Number:0 

Error Message:java.lang.ArithmeticException: / by zero 


Try Catch Block: A method catches exceptions employing a combination of the try and catches keywords. 

A try/catch block is placed around the code which may generate Code at intervals a try/catch block is stated as protected code.

How To Use try-catch-throw in Java?

import java.util.Scanner; 

class icoderweb 


public static void main(String[] args) 


Scanner obj=new Scanner(System.in); 

int x,y,z; 

try 


System.out.println("Enter First Number:"); 

x=obj.nextInt();

System.out.println("Enter Second Number:"); 

y=obj.nextInt();

if(z!=0) 


z=x/y; 

System.out.println("Division Value:"+z); 

}

else

throw new Exception("Do not put zero"); 


catch (Exception e) 


System.out.println("Error Message:"+e); 



}

*****Output*****
 
First Time Run

Enter First Number:36

Enter Second Number:9
 
Division Value=4 

Second Time Run

Enter First Number:78 

Enter Second Number:0 

Error Message:java.lang.Exception:Do not put zero


Finally Block: A final block could be an attempted block or a catch block. A final block of code invariably executes.
it is Using a final block permits you to run any cleanup sort statements that you just need to execute regardless of what happens within the protected code.

How To Use try-catch-throw-finally in Java?

import java.util.Scanner; 

class icoderweb 


public static void main(String[] args) 


Scanner obj=new Scanner(System.in); 

int x,y,z=0; 

try 


System.out.println("Enter First Number:"); 

x=obj.nextInt();

System.out.println("Enter Second Number:"); 

y=obj.nextInt();

if(z!=0) 


z=x/y; 

else

throw new Exception("Do not put zero"); 


catch (Exception e) 


System.out.println("Error Message:"+e); 

}

finally

{

System.out.println("Division Value:"+z); 

}


}

*****Output***** 

First Time Run

Enter First Number:36

Enter Second Number:9 

Division Value=4 

Second Time Run

Enter First Number:78 

Enter Second Number:0 

Error Message:java.lang.Exception:Do not put zero

Division Value:0



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.