Try and catch in Exception Handling.

Try and catch in Exception Handling.

How can we use try and catch block in exception handling?

View Answers

June 8, 2012 at 3:27 PM

Here is an example of Exception handling.

Example:

public class TryCatchExample{
      public static void main(String [] args){
          int a= 10;
          int b= 0;
          int x;
          try{
           x=a/b;
          }catch (Exception er){
              System.out.println("Division by zero");
                    System.out.println(er);
          }
          }
     }

Result Display:

Division by zero
Java.lang.ArithmeticException: / by zero

Description:- We have created a class named TryCatchExample. In the main method, we have declared some variables of integer type. Then to perform exception handling, we have used try and catch block where we have divided the integer value by 0 in the try block. This results in an Arithmetic Exception. The catch block will show this exception.









Related Tutorials/Questions & Answers:
Try and catch in Exception Handling.
Try and catch in Exception Handling.  How can we use try and catch.... Then to perform exception handling, we have used try and catch block where we have...; try{ x=a/b; }catch (Exception er
Exception Handling with and without using try catch block
Description: Without using try catch block. If you do not want to explicitly make try catch block then to you program write throws Exception to your...(exceptionHandle.java:7)  Now the other way to use Exception Handling is by using try
Advertisements
JavaScript Exception Handling
JavaScript Exception Handling: Try...catch block help to handle the exception handling, try..catch block works together.  You use the catch statement.... If no exception occurs, the code inside the catch statement will never
Exception Handling : Multiple Catch
Exception Handling : Multiple Catch In this tutorial, we will discuss the use of declaring multiple catch  with the try block. Multiple Catch : A try... with the same try block, you can raise different types of exceptions. When an exception
Exception handling mechanism
keywords you can handle exception in java they are as follows: try catch... might throw an exception, but try block must followed by catch or finally...Exception handling mechanism In this tutorial you will learn about exception
try catch
); } catch(Exception e) { System.out.println(e...try catch  why following code gives compile time error.please reply...=1;i<=3;i++) { System.out.println(i); try
Java try, catch, and finally
Java try, catch, and finally         The try, catch, and finally keywords are Java keywords... exceptions in Java is achieved through the use of the try and catch blocks. Catch
Exception handling in java
Exception handling in java We are going to discus about Exception handling... when during execution in a program .we are generate of exception try() block and catch() block. We are handle of exception checked and unchecked exception
Multiple try catch
Multiple try catch      ... block then it will be catch by the catch block. We can have more than one try/catch block. The most specific exception which can be thrown is written on the top
Nested try catch
Nested try catch      ... then it will be catch by the catch block. We can have more than one try/catch... specific exception which can be thrown is written on the top in the catch block
java try catch
java try catch  try{ return 1; }catch(exception e){ return 2; } finally{ Return 3; } What is the out put if any exception occurred
Try catch in JSP
Try catch in JSP      ... it is caught inside the catch block. The try catch block in jsp just work as try catch... of Try catch in jsp</TITLE> </HEAD> <BODY> <table align
exception handling
exception handling  What is an exception and what are the advantages of exception handling mechanism.   Java Exception Exception... a separate block of codes. This is done with the help of try-catch blocks. 4
try catch method in java
try catch method in java  try catch method in java - when and how should i use the try and catch method in Java ?   Please visit the following links: http://www.roseindia.net/help/java/t/java-try.shtml http
exception handling
exception handling  explain about exception handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  what is the information of exception handling?   Have a look at the following link: Java Exception Handling
exception handling
exception handling  could you please tell me detail the concept of exception handling
exception handling
exception handling   Give the exception hierarchy of Java.   Please visit the following links: http://www.roseindia.net/java/java-exception/exception-java.shtml http://www.roseindia.net/java/exceptions/exception
Nested Try-Catch Blocks
will handle the exception.  The syntax of nested try-catch blocks is given... Nested Try-Catch Blocks       In Java we can have nested try and catch blocks. It means
ModuleNotFoundError: No module named 'try_catch'
ModuleNotFoundError: No module named 'try_catch'  Hi, My Python... 'try_catch' How to remove the ModuleNotFoundError: No module named 'try... have to install padas library. You can install try_catch python with following
Throw,Throws, n try and Catch
Throw,Throws, n try and Catch  what is the difference between Throw,Throws and Try&Catch
Exception Handling
[]) throws Exception { int id; int grade; String grades = new String(); try{ id...Exception Handling  Hi, I really appreciate the help I have gotten,I... sick mid way through this course and have found it impossible to catch up
Exception Handling
[]) throws Exception { int id; int grade; String grades = new String(); try...Exception Handling  Hi, I really appreciate the help I have gotten,I... sick mid way through this course and have found it impossible to catch up
Exception handling
Exception handling  how to resolve file not found error exception
Exception Handling
Exception Handling  create an exception class, which thros an exception if operand is non-numeric in claculating modules. ( Use command line arguments
Exception handling
Exception handling  exception are occur at run time but when we connect to the database errors will come at compile time why
exception handling
exception handling  can you explain how to handle the exception in java with picture representation
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Exception Handling
Exception Handling  wrtie a java program to enable the user to handle any change of divide by zero exception.   Please visit the following link: http://www.roseindia.net/java/exceptions/how-to-throw-exceptions.shtml
Exception Handling in java
Exception Handling in java  what is advantage to catch smaller exception first and then normal exception. I mean we normally catch SQLException first and then Exception.   Hi, The exceptions should be from
exception Handling
exception Handling  write a program in Java to display the names and roll numbrs of students. Initialize respective array variables for 10 students. Handle ArrayIndexOutOfBoundsException, so that any such problem doesn't cause
exception handling
exception handling  a program to accept only three integer values as command line arguments. Print the values entered by the user. Handle ArrayIndexOutOfBoundsException and NumberFormatException by providing the applied messages
Java exception handling
Java exception handling  What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement
Throw,Throws, n try and Catch
Throw,Throws, n try and Catch  What is the difference between Throw and Throws
Exception Handling - Java Beginners
method instead of try and catch exception handler. For instance, public static...Exception Handling  hi,can u pls make me understand d clear cut difference between throw n throws keyword... n how can we make our own exception..i
Exception Handling
; while (num != -1) { try { num = input.nextInt(); } catch (Exception e...Exception Handling You all are aware of Exceptions and the methods to handle... Exception Handling. Through the given code, you will come to know the use
Java Exception - Exception Handling in Java
Java Exception - Exception Handling in Java  ... and catch the errors using catch() block. Exception turns the direction of normal... code you will see that how the exception handling can be done in java program
Exception Handling in Java
which generate the error in the try{} block and catch the errors using catch... occurred in the program. For more details about exception handling click on the following links Exception Handling in Java Example to show exception
Java exception handling
Java exception handling  what are the constraints imposed by overriding on exception handling
Exception Handling in Struts.
Exception Handling in Struts.  How you can do Exception Handling in Struts

Ads