Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem to learn exceptions.

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    11
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem to learn exceptions.

    Hi all.

    Im trying to learn how to make a own exceptions. I guess it's up to the structure im using etc. I have dropped from 11 errors down to 4. But now i would be pleased to get some help to get this done

     
    import java.util.Scanner; 
    import java.util.*; 
     
    public class omaPoikkeus { 
    	public static void main(String[] args) { 
    		Scanner lukija = new Scanner(System.in); 
    		int testattavaLuku = 0; 
    		boolean syöteOk = true; 
     
    		do { 
    			try { 
    				System.out.print("Anna testattava luku: "); 
    				testattavaLuku = lukija.nextInt(); 
    				syöteOk = true; 
    			} catch (InputMismatchException e) { 
    				System.out.println("Et antanut kokonaislukua!"); 
    				syöteOk = false; 
    				lukija.nextLine(); 
    			} 
    		} while(!syöteOk); 
     
    		try { 
    			testaaArvo(testattavaLuku); 
    			System.out.println("Arvo oli välillä 5-10."); 
    		} catch(PieniPoikkeus e) { 
    			System.out.println("PieniPoikkeus siepattu!"); 
    			tulostaVirheTeksti(e); 
    		} catch(SuuriPoikkeus e) { 
    			System.out.println("SuuriPoikkeus siepattu!"); 
    			tulostaVirheTeksti(e); 
    		}           
    	} 
     
    	// Sinun koodi tulee tähän
    	public static int testaaArvo(int luku) throws PieniPoikkeus, SuuriPoikkeus{
    		if (luku < 5) {
    			throw new PieniPoikkeus("Luku on alle 5.");
    		}
     
    		if (luku > 10) {
    			throw new SuuriPoikkeus("Luku on yli 10.");
    		}
     
    		return luku;
    	}
     
    	public static void tulostaVirheTeksti(String viesti) {
    		System.out.println(viesti);
    	}
     
     
    	// Poikkeukset
    	class PieniPoikkeus extends Exception{
     
    		// Konstruktori
    		public PieniPoikkeus(String exc) {
    			super(exc);
    		}
     
    		public String getMessage() {
    			return super.getMessage();
    		}
    	}
     
    	class SuuriPoikkeus extends Exception{
     
    		// Konstruktori
    		public SuuriPoikkeus(String exc) {
    			super(exc);
    		}
     
    		public String getMessage() {
    			return super.getMessage();
    		}
    	}
    }

    What is the tree where im bangin my head currently?

    omaPoikkeus.java:28: error: method tulostaVirheTeksti in class omaPoikkeus cannot be applied to given types;
    tulostaVirheTeksti(e);
    ^
    required: String
    found: omaPoikkeus.PieniPoikkeus
    reason: actual argument omaPoikkeus.PieniPoikkeus cannot be converted to String by method invocation conversion
    omaPoikkeus.java:31: error: method tulostaVirheTeksti in class omaPoikkeus cannot be applied to given types;
    tulostaVirheTeksti(e);
    ^
    required: String
    found: omaPoikkeus.SuuriPoikkeus
    reason: actual argument omaPoikkeus.SuuriPoikkeus cannot be converted to String by method invocation conversion
    omaPoikkeus.java:38: error: non-static variable this cannot be referenced from a static context
    throw new PieniPoikkeus("Luku on alle 5.");
    ^
    omaPoikkeus.java:42: error: non-static variable this cannot be referenced from a static context
    throw new SuuriPoikkeus("Luku on yli 10.");
    ^
    4 errors
    Process javac exited with code 1
    Last edited by E.K.Virtanen; April 25th, 2013 at 06:25 AM. Reason: solved


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem to learn exceptions.

    Quote Originally Posted by E.K.Virtanen View Post
    What is the tree where im bangin my head currently?
    You tell us. What does this code do? Does it generate any compiler errors? Runtime errors? Does it result in unexpected behavior? Something else? What *exactly* is the problem?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    11
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem to learn exceptions.

    Sorry about mis-information. I added compiler errors as quote in first program.
    I thought the program is so small, that i would not need to explain it's purpose with my poor english. But ill try if it's necessary.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem to learn exceptions.

    required: String
    found: omaPoikkeus.PieniPoikkeus
    The method takes a String as its arg, but the code is calling it with an omaPoikkeus.PieniPoikkeus object.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Location
    Finland
    Posts
    11
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem to learn exceptions.

    Ok. It took few days (and championship of icehockey by my favorite hockey team) but i finally got it.

Similar Threads

  1. [HELP] unreported exceptions
    By Paytheprice in forum Java Theory & Questions
    Replies: 3
    Last Post: January 27th, 2013, 08:01 PM
  2. UserDefined Exceptions
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: May 8th, 2012, 05:39 AM
  3. Methods and Exceptions
    By Mandraix in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2011, 07:15 PM
  4. how to handle many more exceptions
    By shailaja in forum Member Introductions
    Replies: 1
    Last Post: December 27th, 2010, 01:02 AM
  5. How To add my own Exceptions
    By Newtojava in forum Object Oriented Programming
    Replies: 1
    Last Post: September 2nd, 2010, 07:43 AM