Skip to main content

Posts

Showing posts from May, 2023

Explanation of 'try catch finally' block in java programming

 try-catch-finally block in java programming try block To handle a run time error and monitor the results,we simply enclose the code inside a try block.If an exception occurs within the try block,it is handled by the appropriate exception handler ( catch block) associated with the try block.A try block should have one (or more) catch blocks  or one finally block or both.Exception handlers are put in association with a try block by providing one or more catch block directly after the try block Syntax of try catch and finally block try { //java statements(risky code)      }      catch(Exception e)      {      //handled code      }     finally     {          //cleanup code          } catch block A catch block is specified by the keyword catch followed by a single argument within parentheses().A catch block is a group ...

Objective type questions and answers part-2

 Exception Handling Questions and Answers in Java Programming  Objective Type Q&A(part 2) 1:  An exception is an abnormal condition that arises in a block of statements during program execution 2: In java,exceptions are the sub-classes of the built-in class throwable 3: In java an exception is an object that describes an abnormal condition that has occurred in a piece of code. 4: Exception class is sub-classed to create a user-defined exception. 5:Exceptions of the RuntimeException type are automatically defined for java programs. 6: When using multiple catch statements,if the exception sub-classes are not placed before their super-classes unreachable code error occurs. 7: A method specifies the exceptions that are not handled by it using the throws keyword 8: throw keyword is used to throw an exception explicitly 9: OutOfMemory is an example off Error type of exception

Java program and output-Exception handling

E xception handling exercises in java program with output A common exception:divided by zero program1 public class Divide { public static void main(String args[]) { System.out.println("\nPROGRAM EXECUTION START HERE\n"); int NUM1,NUM2,RESULT; NUM1=Integer.parseInt(args[0]); NUM2=Integer.parseInt(args[1]); RESULT=NUM1/NUM2; System.out.println(NUM1+"/"+NUM2+"="+RESULT); System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n"); } } Using try catch block: program2 public class Divide1 { public static void main(String args[]) { System.out.println("\nPROGRAM EXECUTION START HERE\n"); int NUM1,NUM2,RESULT; try { NUM1=Integer.parseInt(args[0]); NUM2=Integer.parseInt(args[1]); RESULT=NUM1/NUM2; System.out.println(NUM1+"/"+NUM2+"="+RESULT); } catch(Exception e) { System.out.println(e); } System.out.println("\nPROGRAM EXECUTION COMPLETE HERE\n"); } } observe the both programs and ...

Exception Handling in java programming

 Exception Handling and types of exception handling in java programming Exception handling in java programming Exception handling means avoiding abnormal termination of the program.Different types of error occur in our program,such as user errors,logic errors or system errors.When an exception occurs, it makes further execution of the program impossible.Java offers an excellent exception handling mechanism which is easy to understand and implement.Whenever an error occurs during the execution of a program,it throws an exception.If the exception is not handled properly, the program execution  is terminated. What is an exception handling mechanism in java? Exception handling is a mechanism that enables programs to detect and handle errors before they occur rather than allowing them to occur and suffering the consequences. Handling Exception In Java  Exception handling in java is accomplished by using  five keywords. try,catch,throw,throws and finally . Java exception ...