Hello, I am new to C#, but I've had some Java experience.
In java you can write a method, throw an exception in it and with a "throws" clause let the method that called it handle the thrown exception, for example:
Is there a similar structure in C# to propagate the exception, or do I have to write a try/catch block into every method that the exception must come through and rethrow the exception at each catch block?
For example:
Thanks,
Komyg
In java you can write a method, throw an exception in it and with a "throws" clause let the method that called it handle the thrown exception, for example:
Code:
public void myMethod1()
{
try
{
myMethod2();
}
catch(MyException e)
{
// Handle Exception
}
}
public void myMethod2() throws MyException
{
throw new MyException("message!");
}
Is there a similar structure in C# to propagate the exception, or do I have to write a try/catch block into every method that the exception must come through and rethrow the exception at each catch block?
For example:
Code:
public void myMethod1()
{
try
{
myMethod2();
}
catch(MyException e)
{
// Handle exception
}
}
public void myMethod2() throws MyException
{
try
{
myMethod3();
}
catch(MyException e)
{
throw new MyException("message!", e);
}
}
public void myMethod3()
{
try
{
throw new MyException("message!");
}
catch(MyException e)
{
throw new MyException("message!", e);
}
}
Thanks,
Komyg