JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java Exception Handling - Multiple Cache Blocks Examples

  • A single try block can have multiple catch blocks. This is required when the try block has statements that generates different types of exceptions.
  • If the first catch block contains the Exception class object then the subsequent catch blocks are never executed.
  • The last catch block in multiple catch blocks must contain the Exception class object. This is because, the java complier gives an error saying that the subsequent catch blocks haven't been reached. This is known as Unreachable code problem.

Multiple Cache Blocks Sample Code

Code:
package com.myjava.exceptions;

import java.net.MalformedURLException;
import java.net.URL;

public class MyMultipleCatchBlocks {
    
    public static void main(String a[]){
        MyMultipleCatchBlocks mmcb = new MyMultipleCatchBlocks();
        mmcb.execute(1);
        mmcb.execute(2);
    }
    
    public void execute(int i){
        try{
            if(i == 1){
                getIntValue("7u");
            } else {
                getUrlObj("www.junksite.com");
            }
        } catch (NumberFormatException nfe){
            System.out.println("Inside NumberFormatException... "+nfe.getMessage());
        } catch (MalformedURLException mue){
            System.out.println("Inside MalformedURLException... "+mue.getMessage());
        } catch (Exception ex){
            System.out.println("Inside Exception... "+ex.getMessage());
        }
    }
    public int getIntValue(String num){
        return Integer.parseInt(num);
    }
    
    public URL getUrlObj(String urlStr) throws MalformedURLException{
        return new URL(urlStr);
    }
}

Example Output

Inside NumberFormatException... For input string: "7u"
Inside MalformedURLException... no protocol: www.junksite.com

Other Exception Handling Examples

Knowledge Centre
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.