7 Replies - 377 Views - Last Post: 26 January 2019 - 03:32 AM Rate Topic: -----

#1 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

Exception check

Posted 24 January 2019 - 09:41 AM

Hi, I have a general question. If I write a method that throws exceptions and another method calls it, can I check if any exception is thrown? Usually I like to handle exceptions separately but let's say I want to terminate the method if any exception had been thrown. It won't work if there had been an exception. Is that possible?
Is This A Good Question/Topic? 0
  • +

Replies To: Exception check

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12525
  • View blog
  • Posts: 45,655
  • Joined: 27-December 08

Re: Exception check

Posted 24 January 2019 - 10:00 AM

Are you familiar with the try-catch block?

See William_Wilson's excellent tutorials on Exception handling:
https://www.dreaminc...-catch-finally/
https://www.dreaminc...g-and-chaining/
https://www.dreaminc...ed-customizing/
Was This Post Helpful? 1
  • +
  • -

#3 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

Re: Exception check

Posted 24 January 2019 - 11:59 AM

View Postmacosxnerd101, on 24 January 2019 - 10:00 AM, said:

Are you familiar with the try-catch block?


I am, indeed. But I am fairly new to this. I looked at your links and I hope I understood it correctly:
If I call a method which could throw two different exceptions. Can I put the call of the method in a try block and add two catch blocks which are executed if that method throws any exception?

For example:

Method example() {
   try {
      methodThatThrowsTwoExceptions();
   }
   catch(FirstException one) {
      System.out.println("First exception hs been thrown!");
   }
   catch(SecondException two) {
      System.out.println("Second exception hs been thrown!");
   }


This post has been edited by overwhelmed_student: 24 January 2019 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 Jordy2254   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 52
  • Joined: 18-October 14

Re: Exception check

Posted 24 January 2019 - 01:24 PM

View Postoverwhelmed_student, on 24 January 2019 - 11:59 AM, said:

I am, indeed. But I am fairly new to this. I looked at your links and I hope I understood it correctly:
If I call a method which could throw two different exceptions. Can I put the call of the method in a try block and add two catch blocks which are executed if that method throws any exception?

For example:

Method example() {
   try {
      methodThatThrowsTwoExceptions();
   }
   catch(FirstException one) {
      System.out.println("First exception hs been thrown!");
   }
   catch(SecondException two) {
      System.out.println("Second exception hs been thrown!");
   }



Yep so the code above is correct, and then your code for the function will be:

public void methodThatThrowsTwoExceptions() throws NumberFormatException, IllegalArgumentException{
		
		int x = 5;
		
		if(x > 5) throw new NumberFormatException();
		else throw new IllegalArgumentException();
	}



And then you throw a new instance of whatever exception it is, and then as many exceptions needed can be added to the function. just seperate each one with a ','

I just realised what you where asking, I will write some better code and submit give me 2 mins
Thanks
Jordan

This post has been edited by Jordy2254: 24 January 2019 - 01:26 PM

Was This Post Helpful? 1
  • +
  • -

#5 Jordy2254   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 52
  • Joined: 18-October 14

Re: Exception check

Posted 24 January 2019 - 01:35 PM

Now i've re-read the scenario the below code will work, it works that if the string passed to the first function it will throw a number format, if not it will throw a illegalargs. and then output the appropriate. I hope this as helped, to make the code more clear the ILLegal args is added on the checkinput however this is not required.

Hope this is what you where looking for

Thanks
Jordan

public class Example {

	public static void main(String[] args) {
		new Example();
	}
	
	public Example() {
		try {
			checkInput("throw");
		} catch (NumberFormatException e) {
			System.out.println("Invalid number");
		}catch (IllegalArgumentException e) {
			System.out.println("Illegal args");
		}
	}
	
	public boolean checkInput(String input) throws NumberFormatException, IllegalArgumentException{
		if (input == "throw") throw new NumberFormatException();
		
		doSomething();
		return false;
	}
	
	public boolean doSomething() throws IllegalArgumentException{
		throw new IllegalArgumentException();
	}
}



Was This Post Helpful? 1
  • +
  • -

#6 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

Re: Exception check

Posted 25 January 2019 - 03:16 AM

Okay, I think I got it so far. I have to catch every exception that could be thrown by any executed method. But what if the exception already gets caught by the method? Can I use catch() twice? Will both catches execute? And if the method also has a finally, will it be executed? I changed your example in doSomething() and checkInput():

public class Example {

	public static void main(String[] args) {
		new Example();
	}
	
	public Example() {
		try {
			checkInput("throw");
		} catch (NumberFormatException e) {
			System.out.println("Invalid number");
		}catch (IllegalArgumentException e) {
			System.out.println("Illegal args");
		}
	}
	
	public boolean checkInput(String input) throws NumberFormatException, IllegalArgumentException{
		if (input == "throw") throw new NumberFormatException();
		
	        try {
                   doSomething();
                }
                catch(IllegalArgumentException) {
                   System.out.println("It always throws he exception!");
                }
                finally {
                   System.out.println("This will be executed no matter what!");
                }
		return false;
	}
	
	public boolean doSomething() throws IllegalArgumentException{
		throw new IllegalArgumentException();
	}
}



Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,829
  • Joined: 13-June 14

Re: Exception check

Posted 25 January 2019 - 03:23 AM

FWIW, IllegalArgumentException and NumberFormatException are both unchecked exceptions (that is, they're subclasses of RuntimeException and so you don't need to declare that checkInput throws them on line 17. In fact, that method isn't ever going to throw IllegalArgumentException, since you're catching it. You probably want to read about checked vs unchecked exceptions at some point.
Was This Post Helpful? 0
  • +
  • -

#8 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

Re: Exception check

Posted 26 January 2019 - 03:32 AM

View Postndc85430, on 25 January 2019 - 03:23 AM, said:

In fact, that method isn't ever going to throw IllegalArgumentException, since you're catching it. You probably want to read about checked vs unchecked exceptions at some point.


Oh, I see! I heard about it but everything is kind of too much since English is not my first language and therefore I don't catch everything in my texts. I will read more into it, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1